Springboo如何t動態(tài)修改配置文件屬性
更新時間:2023年09月16日 15:04:18 作者:kenick
這篇文章主要介紹了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只會加載當前環(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("當前環(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(" ================== 當前配置 =================="); 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ù)庫在在容器初始化時讀取對應數(shù)據(jù)來執(zhí)行特定需求。
好處可以把一些配置文件配置到數(shù)據(jù)庫。用到@PostConstruct注解與@DependesOn注解
@PostConstruct注解
配置了此注解方法會在調用構造方法后自動被調用,也可以理解為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); } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python讀取dicom圖像示例(SimpleITK和dicom包實現(xiàn))
今天小編就為大家分享一篇python讀取dicom圖像示例(SimpleITK和dicom包實現(xiàn)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01pytorch方法測試詳解——歸一化(BatchNorm2d)
今天小編就為大家分享一篇pytorch方法測試詳解——歸一化(BatchNorm2d),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01