Springboo如何t動(dòng)態(tài)修改配置文件屬性
Springboot動(dòng)態(tài)修改配置文件屬性
package com.kenick.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class CustomApplicationProperties implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
MutablePropertySources propertySources = environment.getPropertySources();
// 所有屬性文件名
String env = "";
Iterator<PropertySource<?>> iterator = propertySources.iterator();
System.out.println(" ================== 所有配置文件名 ==================");
while(iterator.hasNext()){
PropertySource<?> next = iterator.next();
String configFileName = next.getName();
System.out.println(configFileName);
if(configFileName.contains("application-")){ // spring只會(huì)加載當(dāng)前環(huán)境配置
env = configFileName.split("application-")[1].replace(".properties]", "");
}
}
// 獲取主配置文件
String mainName = "applicationConfig: [classpath:/application.properties]";
MapPropertySource propertySource = (MapPropertySource) propertySources.get(mainName);
Map<String, Object> source = propertySource.getSource();
System.out.println(" ================== 主配置 ==================");
source.forEach((k,v) -> {
System.out.println(k+":"+v.toString());
});
// 獲取激活配置文件
System.out.println("當(dāng)前環(huán)境:" + env);
String activeName = "applicationConfig: [classpath:/application-" + env + ".properties]";
MapPropertySource activePropertySource = (MapPropertySource) propertySources.get(activeName);
Map<String, Object> activeSource = activePropertySource.getSource();
System.out.println(" ================== 當(dāng)前配置 ==================");
Map<String, Object> newConfigMap = new HashMap<>();
activeSource.forEach((k,v) -> {
System.out.println(k+":"+v.toString());
newConfigMap.put(k, v.toString()); // value必須要放入String格式
});
// 可動(dòng)態(tài)修改配置
// newConfigMap.replace("log.custom.test", "E:\\tmp\\logs");
propertySources.replace(activeName, new MapPropertySource(activeName , newConfigMap));
}
}
org.springframework.boot.env.EnvironmentPostProcessor=com.kenick.config.CustomApplicationProperties
動(dòng)態(tài)給springBoot配置增加屬性
有些時(shí)候我們要把代碼部署到不同地方,根據(jù)地方不同來(lái)辨別要執(zhí)行那些代碼,這時(shí)就要把一些配置事先配置到數(shù)據(jù)庫(kù)在在容器初始化時(shí)讀取對(duì)應(yīng)數(shù)據(jù)來(lái)執(zhí)行特定需求。
好處可以把一些配置文件配置到數(shù)據(jù)庫(kù)。用到@PostConstruct注解與@DependesOn注解
@PostConstruct注解
配置了此注解方法會(huì)在調(diào)用構(gòu)造方法后自動(dòng)被調(diào)用,也可以理解為spring容器初始化的時(shí)候執(zhí)行該方法。
實(shí)例代碼如下
@Component
public class Config3 {
@Autowired
private ConfigurableEnvironment environment;
@PostConstruct
public void setProvinceCode() {
MutablePropertySources propertySources = environment.getPropertySources();
Pattern p = Pattern.compile("^applicationConfig.*");
for (PropertySource<?> propertySource : propertySources) {
if (p.matcher(propertySource.getName()).matches()) {
Properties properties = new Properties();
Map<String, String> map = new HashMap<>();
map.put("code","10000000000");
properties.putAll(map);
PropertiesPropertySource constants = new PropertiesPropertySource("system-config", properties);
propertySources.addAfter(propertySource.getName(),constants);
}
}
}
}@DependesOn注解
用來(lái)表示一個(gè)Bean的實(shí)例化依賴另一個(gè)Bean的實(shí)例化
@DependsOn("config3")
@Component
public class Config2 {
@Value("$[code]")
private String codeValue;
public void show() {
System.out.println(codeValue);
}
@Value("$[code]")
public void setProvinceCode(String codeValue) {
System.out.println("code:"+codeValue);
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python高效實(shí)現(xiàn)刪除PDF中的超鏈接
在數(shù)字文檔時(shí)代,PDF文件因其跨平臺(tái)兼容性和版式固定性,成為信息交換的常用載體,本文將引導(dǎo)你使用一個(gè)高效的Python庫(kù),輕松實(shí)現(xiàn)PDF超鏈接的批量刪除,感興趣的小伙伴可以了解下2025-10-10
我用Python抓取了7000 多本電子書(shū)案例詳解
這篇文章主要介紹了我用Python抓取了7000 多本電子書(shū)案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
從基礎(chǔ)到高級(jí)詳解Python對(duì)象序列化的實(shí)戰(zhàn)指南
在軟件開(kāi)發(fā)中,對(duì)象序列化是一項(xiàng)至關(guān)重要的技術(shù),它允許將內(nèi)存中的復(fù)雜對(duì)象轉(zhuǎn)換為可以存儲(chǔ)或傳輸?shù)母袷?本文將展示如何在不同場(chǎng)景下選擇和應(yīng)用最合適的序列化方案,下面就跟隨小編一起了解下吧2025-09-09
pd.drop_duplicates刪除重復(fù)行的方法實(shí)現(xiàn)
drop_duplicates 方法實(shí)現(xiàn)對(duì)數(shù)據(jù)框 DataFrame 去除特定列的重復(fù)行,本文主要介紹了pd.drop_duplicates刪除重復(fù)行的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Python實(shí)現(xiàn)簡(jiǎn)單的代理服務(wù)器
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單的代理服務(wù)器,可實(shí)現(xiàn)代理服務(wù)器基本的包轉(zhuǎn)發(fā)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
python 利用pywifi模塊實(shí)現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實(shí)時(shí)監(jiān)控網(wǎng)絡(luò)
這篇文章主要介紹了python 利用pywifi模塊實(shí)現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實(shí)時(shí)監(jiān)控網(wǎng)絡(luò),需要的朋友可以參考下2019-09-09
Python Datetime模塊和Calendar模塊用法實(shí)例分析
這篇文章主要介紹了Python Datetime模塊和Calendar模塊用法,結(jié)合實(shí)例形式分析了Python日期時(shí)間及日歷相關(guān)的Datetime模塊和Calendar模塊原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
Python保存環(huán)境方式(導(dǎo)出requirements.txt文件)
這篇文章主要介紹了Python保存環(huán)境方式(導(dǎo)出requirements.txt文件),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

