Spring外部化配置的幾種技巧分享
正文
Envrionment 獲取外部配置
@Log4j2
@SpringBootApplication
public class ConfigurationApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigurationApplication.class, args);
}
@Bean
ApplicationRunner applicationRunner(Environment environment){
return args -> {
log.info("user.name : {}",environment.getProperty("user.name"));
};
}
}
修改Spring默認(rèn)配置文件名稱
啟動(dòng)程序參數(shù)中加入如下配置:
--spring.config.name=app
Value注解配置來(lái)源
配置文件
@Bean
ApplicationRunner applicationRunner(Environment environment,
@Value("${greeting.message:hello boy}") String message){
return args -> {
log.info("from application.properties user.name : {}",environment.getProperty("user.name"));
log.info("from application.properties greeting.message : {}",message);
};
}
默認(rèn)值
value注解通過(guò)冒號(hào)來(lái)配置默認(rèn)值:
@Value("${greeting.message:hello boy}")
獲取環(huán)境變量值
獲取程序參數(shù)值
外部化配置文件優(yōu)先級(jí)問(wèn)題
如果有application.properties在springboot 啟動(dòng)jar包同一目錄,會(huì)優(yōu)先讀取這個(gè)文件中的配置。
Autowire注入ConfigurableEnvrionment
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(ConfigurationApplication.class)
.run(args);
}
@Autowired
void getConfigurableEnvrionment(ConfigurableEnvironment environment) {
environment.getPropertySources().addLast(new MyPropertySource());
}
ApplicationInitialiazer 配置
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(ConfigurationApplication.class)
.initializers(applicationContext ->
applicationContext.getEnvironment().getPropertySources().addLast(new MyPropertySource()))
.run(args);
}
static class MyPropertySource extends PropertySource<String>{
public MyPropertySource() {
super("myproperty");
}
@Override
public Object getProperty(String name) {
if(name.equalsIgnoreCase("author-name")){
return "john";
}
return null;
}
}
然后通過(guò)@Value注解注入獲取author-name:
@Bean
ApplicationRunner applicationRunner(Environment environment,
@Value("${greeting.message:hello boy}") String message,
@Value("${author-name}") String name){
return args -> {
log.info("from application.properties user.name : {}",environment.getProperty("user.name"));
log.info("from application.properties author.name : {}",name);
};
}
總結(jié)
Spring的Environment抽象有很多值得學(xué)習(xí)的地方,期待下一期每日小技巧。
以上就是Spring外部化配置的幾種技巧分享的詳細(xì)內(nèi)容,更多關(guān)于Spring外部化配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
redis setIfAbsent和setnx的區(qū)別與使用說(shuō)明
這篇文章主要介紹了redis setIfAbsent和setnx的區(qū)別與使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java中提供synchronized后為什么還要提供Lock
這篇文章主要介紹了Java中提供synchronized后為什么還要提供Lock,在Java中提供了synchronized關(guān)鍵字來(lái)保證只有一個(gè)線程能夠訪問(wèn)同步代碼塊,下文更多相關(guān)資料需要的小伙伴可以參考一下2022-03-03
springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的使用
單機(jī)的elasticsearch做數(shù)據(jù)存儲(chǔ),必然面臨兩個(gè)問(wèn)題:海量數(shù)據(jù)存儲(chǔ)問(wèn)題、單點(diǎn)故障問(wèn)題,本文重點(diǎn)給大家介紹springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的運(yùn)用,感興趣的朋友跟隨小編一起看看吧2023-10-10
java中l(wèi)ambda(函數(shù)式編程)一行解決foreach循環(huán)問(wèn)題
這篇文章主要介紹了java中l(wèi)ambda(函數(shù)式編程)一行解決foreach循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)現(xiàn)多線程斷點(diǎn)下載
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多線程斷點(diǎn)下載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

