这篇文章给大家分享的是有关Spring注解解析之@ImportResource的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
首先定义一个不会被ComponentScan
扫描到的包outpackage
,如下:
在该包内创建一个类:
package outpackage; import org.springframework.stereotype.Service; @Service public class HelloService1 { public void method1() { System.out.println("class:HelloService1__method:method1"); } }
在资源目录添加配置文件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>
在启动类平级目录或者是子目录添加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 { }
@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的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。