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)文章
Pycharm關(guān)閉控制臺(tái)多余窗口的解決辦法
這篇文章主要介紹了Pycharm關(guān)閉控制臺(tái)多余窗口的解決辦法,文中通過(guò)圖文結(jié)合的方式講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12
python讀取dicom圖像示例(SimpleITK和dicom包實(shí)現(xiàn))
今天小編就為大家分享一篇python讀取dicom圖像示例(SimpleITK和dicom包實(shí)現(xiàn)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
利用Python和PyQt5構(gòu)建一個(gè)多功能PDF轉(zhuǎn)換器
在日常工作中,處理PDF文件幾乎是每個(gè)人都不可避免的任務(wù),本文將通過(guò)Python和PyQt5搭建一個(gè)強(qiáng)大的PDF文件處理平臺(tái),希望對(duì)大家有所幫助2024-12-12
pytorch方法測(cè)試詳解——?dú)w一化(BatchNorm2d)
今天小編就為大家分享一篇pytorch方法測(cè)試詳解——?dú)w一化(BatchNorm2d),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Django ORM框架的定時(shí)任務(wù)如何使用詳解
這篇文章主要給大家介紹了關(guān)于Django ORM框架的定時(shí)任務(wù)如何使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
python進(jìn)行TCP端口掃描的實(shí)現(xiàn)
這篇文章主要介紹了python進(jìn)行TCP端口掃描的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

