environment与@ConfigurationProperties的关系 加载过程分析

 

 environment是在printBanner之前就初始化好了, 更在context创建之前, 

已经加载application-xxxx.properties, System.properties, System.environment ...

也可以自己监听应用启动 SpringApplicationRunListener事件, 完成自己的独特的配置加载方案

启动后调用listener.finished() 打印一些启动后的信息

 

prepareEnvironment()源码如下
 1     private ConfigurableEnvironment prepareEnvironment(
 2             SpringApplicationRunListeners listeners,
 3             ApplicationArguments applicationArguments) {
 4         // Create and configure the environment
 5         ConfigurableEnvironment environment = getOrCreateEnvironment();
 6 
 7 //加载标准目录下的配置文件 &profile
 8         configureEnvironment(environment, applicationArguments.getSourceArgs());
 9 //加载自定义的配置方案
10         listeners.environmentPrepared(environment);
11         if (!this.webEnvironment) {
12             environment = new EnvironmentConverter(getClassLoader())
13                     .convertToStandardEnvironmentIfNecessary(environment);
14         }
15         return environment;
16     }

 

 

 

@ConfigurationProperties  使用的变量也是从environment中取的

 

 CLI参数配置方式:

 

 

spring boot默认可以加载当前启动路径{user.dir}下 /config目录中的配置文件, /home/config和docker没有关系, 在一些自动划构建工具可能用到这个特性

 

 

 

 

参考:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding

https://www.baeldung.com/properties-with-spring

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-application-property-files

posted @ 2018-11-12 20:08  funny_coding  阅读(1975)  评论(0编辑  收藏  举报
build beautiful things, share happiness