spring boot 自定义属性覆盖application文件属性
参考
Spring boot源码分析-ApplicationListener应用环境:
https://blog.csdn.net/jamet/article/details/78042486
加载application资源文件源码分析:
https://blog.csdn.net/liaokailin/article/details/48878447
ConfigFileApplicationListener 主要实现了以下接口
EnvironmentPostProcessor:用于环境的后处理
SmartApplicationListener:是ApplicationListener的扩展,进一步暴露事件类型。
Ordered:用于将对象排序
ConfigFileApplicationListener类的解释
通过类上的注释,我们可以知道关于该类的一些信息,
1.他默认会从classpath: 、file:./ 、classpath:config/ 、 file:./config/ 加载'application.properties' 和/或 'application.yml'
2.其他配置也会根据active profiles 进行 加载 ,
如 active 此时被设置成 web, spring 加载的时候也会去加载 application-web.properties 和 application-web.yml
加载项目配置文件时,对应 propertySources 的名称如下:
[bootstrap,commandLineArgs,systemProperties,systemEnvironment,
random,servletConfigInitParams,servletContextInitParams,
jndiProperties,applicationConfig: [classpath:/application-console_dev.properties],applicationConfig:
[classpath:/config/application.properties],applicationConfig:
[classpath:/application.properties],bootstrapProperties,applicationConfig:
[classpath:/bootstrap.properties],Management
Server,applicationConfigurationProperties,
defaultProperties,springCloudClientHostInfo]
如果在 applicationConfig 名称的前面添加属性,则项目配置文件中的属性不会覆盖
public class ConsoleDomainPostrocessor implements EnvironmentPostProcessor, Ordered {
private static final String PROPERTY_SOURCE_NAME = "xxProperties";
public static final DOMAIN = "domain";
private int order = ConfigFileApplicationListener.DEFAULT_ORDER + 20;
private static Map<String, String> consoleUrlList = new HashMap<>();
static {
consoleUrlList.put("consoletest", "http://ecc.consoletest.jcloudec.com");
}
@Override
public int getOrder() {
return order;
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> map = new HashMap<>();
EccConsoleProperties target = new EccConsoleProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(target,
EccConsoleProperties.ECC_CONSOLE);
binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));
//默认开启
boolean enabled = target.isEnabled();
if (enabled) {
if (environment.getActiveProfiles().length > 0 &&
(!Arrays.asList(environment.getActiveProfiles()).contains("default"))) {
String[] activeProfiles = environment.getActiveProfiles();
String curentConsoleUrl = consoleUrlList.get(activeProfiles[0]);
if (StringUtils.isNotBlank(curentConsoleUrl)) {
map.put(DOMAIN, curentConsoleUrl);
} else if (StringUtils.isNotBlank(target.getDomain())) {
map.put(DOMAIN, target.getDomain());
} else {
map.put(DOMAIN, "http://xx.com");
}
System.out.println(String.format("activeProfiles:[%s],console domain:[%s]", activeProfiles[0], map.get(CC_CONSOLE_DOMAIN)));
MapPropertySource propertySource = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
// 将属性添加到 application 文件前,这样application 就不会覆盖属性了
environment.getPropertySources().addBefore(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME, propertySource);
}
}
}
}
在 META-INF/spring.factories 文件内容添加
org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.ConsoleDomainPostrocessor
博客所有内容仅供自已学习和学习过程的记录,如有侵权,请联系我删除!!!