参考

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文件属性的更多相关文章

  1. Spring Boot 的配置文件application.properties

    Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...

  2. Spring Boot加载application.properties配置文件顺序规则

    SpringApplication会从以下路径加载所有的application.properties文件: 1.file:./config/(当前目录下的config文件夹) 2.file:./(当前 ...

  3. spring boot 无法读取application.properties问题

    spring boot 无法读取application.properties问题 https://bbs.csdn.net/topics/392374488 Spring Boot 之注解@Compo ...

  4. 一:Spring Boot 的配置文件 application.properties

    Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...

  5. IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示

    在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题.不过我认为更优雅的方式是定义自己的属 ...

  6. Spring Boot 中配置文件application.properties使用

    一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...

  7. Spring Boot 自定义属性 以及 乱码问题

    自定义属性 使用随机数及自定义配置类 中文乱码问题 1添加配置 2设置文件类型 1IDEA 2eclipse 自定义属性 application.properties提供自定义属性的支持,这样我们就可 ...

  8. Spring Boot中配置文件application.properties使用

    一.配置文档配置项的调用 启动后在浏览器直接输入http://localhost:18080/user/test,就直接打印出配置文件中的配置内容. 二.绑定对象bean调用 有时候属性太多了,一个个 ...

  9. 第二篇:彻底搞清楚 Spring Boot 的配置文件 application.properties

    前言 在Spring Boot中,配置文件有两种不同的格式,一个是properties,另一个是yaml. 虽然properties文件比较常见,但是相对于properties而言,yaml更加简洁明 ...

随机推荐

  1. Docker 入门 到部署Web 程序- (阿里面试常用的docker命令和优点)

    最近阿里的面试官问我Docker是做什么用的,我记得之前360和美团,京东的都问过,但是一直没时间看,最近有时间了,系统的学习了一下Docker,在此做一下记录,方便各位看官学习交流 一.Docker ...

  2. oracle 关于表数据delete 后如何恢复

    今天在PL/SQL中操作不小心删掉了某个表的部分数据,这可吓坏了本猿:于是悄悄的打开电脑,赶紧找度娘帮忙.经过度娘的小爬虫帮助,几分钟就把数据恢复了. 那么表数据delete掉后怎么恢复呢? 用fla ...

  3. Web模块:spring-boot-starter-web

    spring-boot-autoconfigure-1.5.1.RELEASE.jar!/org/springframework/boot/autoconfigure/web 上述jar的web包下, ...

  4. Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

    题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...

  5. 【loj6307】「雅礼国庆 2017 Day1」Clique 贪心

    题目描述 数轴上有 $n$ 个点,第 $i$ 个点的坐标为 $x_i$ 权值为 $w_i$ .两个点 $i,j$ 之间存在一条边当且仅当 $|x_i−x_j|\le w_i+w_j$ . 你需要求出这 ...

  6. Day21-自定义simple_tag/filter

    一. 在模板里面对用户发过来的数据进行二次加工.想办法把Python的函数放到模板语言里面使用. 有2种方法:filter与simple_tag 二. 实例 {{name|lower}} trunca ...

  7. ORM框架SQLAlchemy使用学习

    参考源:http://blog.csdn.net/fgf00/article/details/52949973 一.ORM介绍 如果写程序用pymysql和程序交互,那是不是要写原生sql语句.如果进 ...

  8. 洛谷P2125图书馆书架上的书 题解报告

    题目描述 图书馆有n个书架,第1个书架后面是第2个书架,第2个书架后面是第3个书架……第n-1个书架后面是第n个书架,第n个书架后面是第1个书架,第i个书架上有b[i]本书.现在,为了让图书馆更美观, ...

  9. poj3469 Dual Core CPU

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 25576   Accepted: 11033 ...

  10. idea plugin 插件开发之检测文件修改

    实现 ApplicationComponent,BulkFileListener 接口,当然由于是 ApplicationComponent,因此需要在 plugin.xml 加上相关配置. plug ...