當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot-@ImportResource注解
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot-@ImportResource注解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
@ImportResource:導入Spring的配置文件,讓配置文件里面的內容生效
第一步:創建一個spring配置文件bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="helloService" class="com.zsh.springboot02.service.HelloService"/> </beans>第二步:創建HelloService類
public class HelloService { }第三步:測試(沒有使用@ImportResource注解)
@SpringBootTest class SpringBoot02ApplicationTests {@AutowiredApplicationContext ioc;//注入ioc容器@Testpublic void testHelloService(){boolean helloService = ioc.containsBean("helloService");System.out.println(helloService);} }結果:false
從結果我們可以得出Spring的配置文件bean.xml沒有唄加載
這說明Spring Boot里面沒有Spring的配置文件,我們手動編寫的配置文件,也不能自動識別,所以需要使用@ImportResource注解標注在主配置類上
再次測試
結果:true
總結
以上是生活随笔為你收集整理的Spring Boot-@ImportResource注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot-@Propert
- 下一篇: Spring Boot-@Configu