一、前言
最近,关于SpringBoot的配置文件在不同目录下的配置文件出现了相同的配置属性,对于加载顺序有点疑惑。所以写一篇博客记录一下。
二、摘要
关于本片文章阅读后将回答下面几个问题:
1、springBoot何时加载配置文件?
2、springBoot加载配置文件时,不同位置及不同后缀的配置文件如何加载?
3、springBoot的{prifile}文件时是如何加载的?如:(application-dev.yml)
4、不同位置的配置文件配置了相同的属性时 将如何加载?
5、不同位置到配置文件,设置不同属性时,后加载的文件是否会生效?
三、环境搭建
SpringBoot使用的版本是1.5.8.RELEASE
四、启动
通过执行springBoot的主程序启动。下面开始讲解配置文件是何时被加载。进入run中,(关于下面方法的作用可以参考springBoot内置Tomcat启动原理)
public ConfigurableApplicationContext run(String… args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
/**
*
* 准备容器环境、这里会加载配置文件。在这个方法里面会调用所有监听器Listener的onApplicationEvent(event);
* 此时有一个与配置文件相关的监听器就会被加载`ConfigFileApplicationListener`
**/
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
ConfigFileApplicationListener这个监听器会在容器准备环境时被调用,那么我们重点关注一下这个类
/**
* 监听器的这个方法最终将被调用
**/
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
// 加载属性到环境中
addPropertySources(environment, application.getResourceLoader());
configureIgnoreBeanInfo(environment);
bindToSpringApplication(environment, application);
}
1
2
3
4
5
6
7
8
9
10
11
加载的时候会调用一个Load()方法如下:
1.配置文件所在的位置:
默认值为:classpath:/, classpath:/config/, file:./, file:./config/ 优先级从低到高,最先加载file:./config/ 位置的配置文件;如果配置了spring.config.location属性则取这个属性的值。
加载顺序如下:
file:./config/
file:./
classpath:/config/
classpath:/
2.配置文件的名字
默认值为:application;如果配置了spring.config.name属性则取改属性值。
继续进入load方法中;
所有的扩展名:
properties xml yml yaml
加载顺序如下:
proeprties
xml
yml
yaml
加载时候到的拼接过程为 location + name + “.” +ext
location : 指的就是上面提到配置文件所在的位置。
name: 指的就是配置文件的名称 默认就是appliction
ext: 指的就是后缀名。
然后对拼接后的资源路径进行加载。
小结:
上面加载的总体过程就是两层循环,外层控制加载配置文件位置的顺序,在相同位置时,内层就是控制后缀名不同时的加载顺序。
spring.profiles.active 何时起作用?
我们在配置文件中常常会设置一个active属性,用来描述项目中激活使用哪个配置文件。其实当读取配置文件内的属性时(读取规则👆),会首先读取spring.profiles.active 然后将该配置的属性取出来放在 profiles。在后续的加载中,当发现 profiles有值是就会重新加载active指定的文件,(当已经存在profile文件时,就不会读取) 具体代码如下:
放入到队列第一个位置后会重新进行读取。如下图所示
最后又根据最初到那种加载规则一直找到存在的配置文件,我设置的profile 属性为 dev
当找到profile文件时,就会对该文件进行装载并读取属性。并且如果之前已经存在profile文件时将
属性读取时是如何读取的?
可以看到根据配置文件当不同,存在两个配置文件当加载器。以Yaml加载器为例,内部当加载逻辑时,当之前已经被加载过当属性,在后面就不会再被加载。
五、总结
1、springBoot何时加载配置文件?
答:在容器启动时,会有方法调用所以监听器的onApplicationEvent方法。然后与配置加载相关的ConfigFileApplicationListener的对应方法就会被加载,执行读取配置文件操作。
2、springBoot加载配置文件时,不同位置及不同后缀的配置文件如何加载?
答 加载顺序如下:
file:./config/
file:./
classpath:/config/
classpath:/
proeprties
xml
yml
yaml
3、springBoot的 {prifile}文件时是如何加载的?
答 每次加载配置文件时都会先读取 spring.profiles.active属性,如果存在则将该配置文件装载到 加载队列的首位,然后在下次循环时进行寻找加载。
4、不同位置的配置文件配置了相同的属性时 将如何加载?
当无spring.profiles.active 时, 先读取到的属性会先生效。例如:file:./config/配置端口 8080 ;file:./配置端口8081;classpath:/config/配置端口8082 ;classpath:/配置的端口为8083 。根据上文的讲解最先读取到的时 file:./config/的8080,后面再次读取后该属性并不会生效。所以最终的配置端口为8080 (并不是后读取到到配置文件不加载,会加载;但是不生效)
当有spring.profiles.active 时,配置了相同属性时是profiles上的配置生效。例如:file:./config/上的yml配置了端口 8080 但是存在 spring.profiles.active属性 dev;然后我们在dev上也配置来一个端口 8081.此时生效的为8081.
5、不同位置到配置文件,设置不同属性时,后加载的文件是否会生效?
答 会有效果。会是互补配置。即先加载先生效,后加载的不同配置产生互补效果。例如:file:./config/配置了端口8080;file:./配置了访问路径/path。最总二者的配置会达成并集的效果。即二者都生效。
————————————————
版权声明:本文为CSDN博主「莫失莫忘hh」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43732955/article/details/106600869