025 使用@Profile完成环境条件注入
生活随笔
收集整理的這篇文章主要介紹了
025 使用@Profile完成环境条件注入
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一 . 概述
在之前我們說過@Conditional注解可以幫助我們實(shí)現(xiàn)條件的Bean的注冊,但有時(shí)候卻不是很方便.
如我們在生產(chǎn)和測試環(huán)境是不同的,因此我們需要一個(gè)能夠根據(jù)環(huán)境注入Bean的方式.
@Profile注解就能幫助我們實(shí)現(xiàn)這個(gè)功能.
?二 . 測試?
配置類:
@Configuration public class ProfileConfig {@Bean("value")@Profile("test")public String test() {return "test"; } @Bean @Profile("dev") public String dev() { return "dev"; } }我們在test環(huán)境下會注入一個(gè)test的Bean,而在dev環(huán)境下會注入一個(gè)dev的Bean.
測試類:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes= {ProfileConfig.class}) public class ProfileTest {@Autowired@Qualifier("value")private String value;@Testpublic void test() { System.out.println(value); } }我們使用系統(tǒng)的環(huán)境變量,現(xiàn)在使用的環(huán)境是test環(huán)境.
spring會根據(jù)我們制定的系統(tǒng)運(yùn)行參數(shù)注入不同的Bean.
?
轉(zhuǎn)載于:https://www.cnblogs.com/trekxu/p/9094885.html
總結(jié)
以上是生活随笔為你收集整理的025 使用@Profile完成环境条件注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python--装饰器(附偏函数、断言)
- 下一篇: 20172332 2017-2018-2