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
spring boot 自定义属性覆盖application文件属性的更多相关文章
- Spring Boot 的配置文件application.properties
Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...
- Spring Boot加载application.properties配置文件顺序规则
SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...
- spring boot 无法读取application.properties问题
spring boot 无法读取application.properties问题 https://bbs.csdn.net/topics/392374488 Spring Boot 之注解@Compo ...
- 一:Spring Boot 的配置文件 application.properties
Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...
- IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示
在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题.不过我认为更优雅的方式是定义自己的属 ...
- Spring Boot 中配置文件application.properties使用
一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...
- Spring Boot 自定义属性 以及 乱码问题
自定义属性 使用随机数及自定义配置类 中文乱码问题 1添加配置 2设置文件类型 1IDEA 2eclipse 自定义属性 application.properties提供自定义属性的支持,这样我们就可 ...
- Spring Boot中配置文件application.properties使用
一.配置文档配置项的调用 启动后在浏览器直接输入http://localhost:18080/user/test,就直接打印出配置文件中的配置内容. 二.绑定对象bean调用 有时候属性太多了,一个个 ...
- 第二篇:彻底搞清楚 Spring Boot 的配置文件 application.properties
前言 在Spring Boot中,配置文件有两种不同的格式,一个是properties,另一个是yaml. 虽然properties文件比较常见,但是相对于properties而言,yaml更加简洁明 ...
随机推荐
- 第210天:node、nvm、npm和gulp的安装和使用详解
一.node 1.什么是node? 它不是JS文件,也不是JS框架,而是Server side JavaScript runtime,当服务端的一个JS文件运行时,会被NODE拦截,在NODE中运行J ...
- css实现 显示一行文字,超出用...代替
overflow:hidden; white-space:nowrap; text-overflow:ellipsis;
- Socket 传一幅图片给另一个终端
练习Socket传文件,先添加一个组件,简化socket发送和接收文件, 获取IP和端口的类 public static class AddressHelper { /// <summary&g ...
- NetBeans IDE驱动报错The path to the driver executable must be set by the web driver.chrome.driver.system property......
问题:defaulstUserDataPath=C:\\Users\\user1\\AppData\\Local\\Google\\Chrome\\User Data\\Defaul 编译失败 解决 ...
- 【JavaScript】时间戳转日期格式
时间戳: 1480570979000 $.ajax({ url : "getOrderMsg?shiplabel="+ shiplabel, type : "get&qu ...
- 【NOIP2017】宝藏(状态压缩,动态规划)
[NOIP2017]宝藏(状态压缩,动态规划) 题面 洛谷 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 n 个深埋在地下的宝藏屋, 也给出了这 n 个宝藏屋之间可供开发的 m 条道路 ...
- BZOJ4070 [Apio2015]雅加达的摩天楼 【分块 + 最短路】
题目链接 BZOJ4070 题解 考虑暴力建图,将每个\(B_i\)向其能到的点连边,复杂度\(O(\sum \frac{n}{p_i})\),当\(p\)比较小时不适用 考虑优化建图,每个\(dog ...
- Cisco Smart Install远程命令执行漏洞
0x01前言 在Smart Install Client代码中发现了基于堆栈的缓冲区溢出漏洞,该漏洞攻击者无需身份验证登录即可远程执行任意代码.cisco Smart Install是一种“即插即用” ...
- 修改apache的注册表值提高系统权限
In Windows environments when a service is registered with the system a new key is created in the reg ...
- 洛谷P2253 好一个一中腰鼓!
题目背景 话说我大一中的运动会就要来了,据本班同学剧透(其实早就知道了),我萌萌的初二年将要表演腰鼓[喷],这个无厘头的题目便由此而来. Ivan乱入:“忽一人大呼:‘好一个安塞腰鼓!’满座寂然,无敢 ...