您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。