日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 动态注入的两种方式

發(fā)布時間:2023/12/31 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 动态注入的两种方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過@Profile+spring.profiles.active

spring.profiles.active:官方解釋是激活不同環(huán)境下的配置文件,但是實際測試發(fā)現(xiàn)沒有對應的配置文件也是可以正常執(zhí)行的。那就可以把這個key當作一個參數(shù)來使用
@Profile:spring.profiles.active中激活某配置則在spring中托管這個bean,配合@Component,@Service、@Controller、@Repository等使用

@Component @Profile("xx") public class XxxTest extends BaseTest {public void test(){System.out.println("in XxxTest ");} } @Component @Profile("yy") public class YyyTest extends BaseTest {public void test(){System.out.println("in YyyTest ");} }@Service public class MyService {@Autowiredprivate BaseTest test;public void printConsole(){test.test();} }//配置文件激活某個環(huán)境則test就會注入哪個bean spring.profiles.active=xx

通過@Configuration+@ConditionalOnProperty

@Configuration:相當于原有的spring.xml,用于配置spring
@ConditionalOnProperty:依據(jù)激活的配置文件中的某個值判斷是否托管某個bean,org.springframework.boot.autoconfigure.condition包中包含很多種注解,可以視情況選擇

@Configuration public static class ContextConfig { @Autowired private XxxTest xxTest; @Autowired private YyyTest yyTest;@Bean @ConditionalOnProperty(value = "myTest",havingValue = "xx")public BaseTest xxxTest() {return xxTest;}@Bean @ConditionalOnProperty(value = "myTest",havingValue = "yy")public BaseTest yyyTest() {return yyTest;} //配置文件中控制激活哪個bean myTest=xx }

參考資料

https://blog.csdn.net/wild46cat/article/details/71189858
https://www.javacodegeeks.com/2013/10/spring-4-conditional.html

總結

以上是生活随笔為你收集整理的Spring Boot 动态注入的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。