Springboo如何t動態(tài)修改配置文件屬性
Springboot動態(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只會加載當(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格式
});
// 可動態(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
動態(tài)給springBoot配置增加屬性
有些時候我們要把代碼部署到不同地方,根據(jù)地方不同來辨別要執(zhí)行那些代碼,這時就要把一些配置事先配置到數(shù)據(jù)庫在在容器初始化時讀取對應(yīng)數(shù)據(jù)來執(zhí)行特定需求。
好處可以把一些配置文件配置到數(shù)據(jù)庫。用到@PostConstruct注解與@DependesOn注解
@PostConstruct注解
配置了此注解方法會在調(diào)用構(gòu)造方法后自動被調(diào)用,也可以理解為spring容器初始化的時候執(zhí)行該方法。
實例代碼如下
@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注解
用來表示一個Bean的實例化依賴另一個Bean的實例化
@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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
從基礎(chǔ)到高級詳解Python對象序列化的實戰(zhàn)指南
在軟件開發(fā)中,對象序列化是一項至關(guān)重要的技術(shù),它允許將內(nèi)存中的復(fù)雜對象轉(zhuǎn)換為可以存儲或傳輸?shù)母袷?本文將展示如何在不同場景下選擇和應(yīng)用最合適的序列化方案,下面就跟隨小編一起了解下吧2025-09-09
pd.drop_duplicates刪除重復(fù)行的方法實現(xiàn)
drop_duplicates 方法實現(xiàn)對數(shù)據(jù)框 DataFrame 去除特定列的重復(fù)行,本文主要介紹了pd.drop_duplicates刪除重復(fù)行的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
python 利用pywifi模塊實現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實時監(jiān)控網(wǎng)絡(luò)
這篇文章主要介紹了python 利用pywifi模塊實現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實時監(jiān)控網(wǎng)絡(luò),需要的朋友可以參考下2019-09-09
Python Datetime模塊和Calendar模塊用法實例分析
這篇文章主要介紹了Python Datetime模塊和Calendar模塊用法,結(jié)合實例形式分析了Python日期時間及日歷相關(guān)的Datetime模塊和Calendar模塊原理、用法及操作注意事項,需要的朋友可以參考下2019-04-04
Python保存環(huán)境方式(導(dǎo)出requirements.txt文件)
這篇文章主要介紹了Python保存環(huán)境方式(導(dǎo)出requirements.txt文件),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

