一、简介
springBoot 有两种配置文件,一种是 bootstrap(.yml 或者 properties),一种是 application(.yml 或者 properties) 。
二、作用
bootstrap(.yml/.properties) 文件也是 SpringBoot 的默认配置文件,而且其加载的时间相比于 application(.yml/.properties) 更早。
application(.yml/.properties) 和 bootstrap(.yml/.properties) 虽然都是 SpringBoot 的默认配置文件,但是定位却不相同。
bootstrap(.yml/.properties) 可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。
application(.yml/.properties) 可以用来定义应用级别的参数。
如果该项目继承注册中心和配置中心,最好声明在 bootstrap(.yml/.properties) 文件中。
三、优先级
在同一级目录下,会先比较前缀 bootstrap 和 application,其中 bootstrap 比 application 的优先级高,其次再去比较后缀 .properties 和 .yml,其中 .properties 比 .yml 优先级高。
所以它们的执行顺序如下:
bootstrap.properties > bootstrap.yml > application.properties > application.yml
四、配置覆盖
4.1 相同类型比较 properties 优先级高
当同一个配置属性在 bootstrap.properties 和 bootstrap.yml 文件中都存在时,那么 properties 中的配置会被加载,而忽略 yml 文件中的配置(即优先级高的配置覆盖优先级低的配置),不同配置相互互补。此处 application(.properties/yml) 同理。
4.2 不同类型比较 applicatioin 覆盖 bootstrap
但当同一配置在 bootstrap 和 application 中都存在时,那么虽然优先加载 bootstrap 但是会被 applicatioin 中的配置覆盖,此时则变成了低优先级覆盖高优先的配置,所以网上很多文章所说的高优先级覆盖低优先级其实是不严谨的。
4.3 application.properties 覆盖其他三种配置文件
当同一个配置属性在四种配置(bootstrap.properties、bootstrap.yml、application.properties、application.yml)都存在,application.properties 里的配置一定是会覆盖其他配置文件。
首先遵循第二点:虽然优先加载 bootstrap 但是会被 applicatioin 中的配置覆盖,这时候就只剩 application.yml 和 application.properties 了,其次遵循第一点:即优先级高的配置覆盖优先级低的配置,所以 application.properties 的配置文件会覆盖其他配置文件。
五、覆盖验证
5.1 验证相同类型比较 properties 优先级高
准备 bootstrap.properties 和 bootstra.yml ,最后查看哪个端口被占用
# bootstrap.properties
server.port=8888
# bootstrap.yml
server
port: 7777
最后结果,如下图,可以得出结论配置文件前缀都相同的情况下,优先级高(.properties)的配置覆盖优先级低(.yml)的配置
如果创建的 bootstrap 文件未生效的话,请参考以下的 maven 依赖
<!-- 继承springboot,不加这个parent标签就会报错 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
5.2 验证不同类型比较 applicatioin 覆盖 bootstrap
准备 bootstrap.properties 和 application.properties,最后查看哪个端口被占用。
# bootstrap.properties
server.port=8888
# application.properties
server.port=6666
最后结果,如下图,当同一配置在 bootstrap 和 application 中都存在时,优先加载 bootstrap 但是会被 applicatioin 中的配置覆盖。
5.3 验证 application.properties 覆盖其他三种配置文件
准备 bootstrap.properties、bootstrap.yml、application.properties 和 application.yml 四种文件,最后查看哪个端口被占用。
# bootstrap.properties
server.port=8888
# bootstrap.yml
server
port: 7777
# application.properties
server.port=6666
# application.yml
server
port: 5555
最后结果如下图,同一个配置属性在四种配置都存在,application.properties 里的配置一定是会覆盖其他配置文件。