Spring注解解析之@ImportResource
一、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注解内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
Spring注解之@Autowired、@Qualifier、@Resource、@Value
-
spring源码深度解析— IOC 之 默认标签解析(上)
-
spring源码深度解析— IOC 之 循环依赖处理
-
一起学Spring之注解和Schema方式实现AOP
-
【死磕 Spring】----- IOC 之解析 bean 标签:开启解析进程
-
spring源码之bean解析
-
Spring系列之——springboot解析resources.application.properties文件
-
spring源码深度解析— IOC 之 开启 bean 的加载
-
spring源码深度解析— IOC 之 默认标签解析(下)
-
Spring纯注解配置实现代码示例解析