Springboo如何t动态修改配置文件属性
作者:kenick
这篇文章主要介绍了Springboo如何t动态修改配置文件属性问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
Springboot动态修改配置文件属性
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只会加载当前环境配置 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("当前环境:" + 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格式 }); // 可动态修改配置 // newConfigMap.replace("log.custom.test", "E:\\tmp\\logs"); propertySources.replace(activeName, new MapPropertySource(activeName , newConfigMap)); } }
org.springframework.boot.env.EnvironmentPostProcessor=com.kenick.config.CustomApplicationProperties
动态给springBoot配置增加属性
有些时候我们要把代码部署到不同地方,根据地方不同来辨别要执行那些代码,这时就要把一些配置事先配置到数据库在在容器初始化时读取对应数据来执行特定需求。
好处可以把一些配置文件配置到数据库。用到@PostConstruct注解与@DependesOn注解
@PostConstruct注解
配置了此注解方法会在调用构造方法后自动被调用,也可以理解为spring容器初始化的时候执行该方法。
实例代码如下
@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); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。