一、importresource

1.1 定义包和类

首先定义一个不会被componentscan扫描到的包outpackage,如下:

在该包内创建一个类:

package outpackage;

import org.springframework.stereotype.service;

@service
public class helloservice1 {
    public void method1() {
        system.out.println("class:helloservice1__method:method1");
    }
}

1.2 定义配置文件

在资源目录添加配置文件applicationcontext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 开启注解模式 -->
    <context:annotation-config/>

    <!-- 基于注解自动注册spring bean -->
    <context:component-scan base-package="outpackage"/>
</beans>

1.3 定义java config类

在启动类平级目录或者是子目录添加java config类保证能够被springboot扫描到,引入xml配置,如下:

package dongshi.daddy;

import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.importresource;

@configuration
@importresource({"classpath:applicationcontext.xml"})
public class outerpackageconfiguration {
}

1.4 测试代码

@springbootapplication
public class helloworldmainapplication {

    public static void main(string[] args) throws urisyntaxexception, ioexception {
        configurableapplicationcontext run = springapplication.run(helloworldmainapplication.class, args);
        // 获取通过配置文件定义而被扫描到的类
        helloservice1 bean = run.getbean(helloservice1.class);
        system.out.println(bean);
    }
}

二、运行

2021-05-19 17:52:52.896  info 16232 — [           main] o.s.b.w.embedded.tomcat.tomcatwebserver  : tomcat started on port(s): 8083 (http) with context path ”
…snip…
outpackage.helloservice1@1929425f

为了证明确实是xml配置文件在起作用,而不是springboot自己扫描注册,修改配置类,注释掉@importresource({"classpath:applicationcontext.xml"}),如下:

package dongshi.daddy;

import org.springframework.context.annotation.configuration;

@configuration
//@importresource({"classpath:applicationcontext.xml"})
public class outerpackageconfiguration {
}

然后运行:

2021-05-19 18:01:10.522  info 18260 — [           main] dongshi.daddy.helloworldmainapplication  : started helloworldmainapplication in 0.944 seconds (jvm running for 1.355)
exception in thread “main” org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type ‘outpackage.helloservice1’ available
 at org.springframework.beans.factory.support.defaultlistablebeanfactory.getbean(defaultlistablebeanfactory.java:351)
 at org.springframework.beans.factory.support.defaultlistablebeanfactory.getbean(defaultlistablebeanfactory.java:342)
 at org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:1123)
 at dongshi.daddy.helloworldmainapplication.main(helloworldmainapplication.java:16)

可以看到就找不到对应的bean了。

到此这篇关于spring注解解析之@importresource的文章就介绍到这了,更多相关@importresource注解内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!