> 文章列表 > bootstrap与application的优先级及配置覆盖

bootstrap与application的优先级及配置覆盖

bootstrap与application的优先级及配置覆盖

作用

bootstrap(.yml/.properties)文件也是Spring Boot的默认配置文件,而且其加载的时间相比于application(.yml/.properties)更早

application(.yml/.properties)和bootstrap(.yml/.properties)虽然都是Spring Boot的默认配置文件,但是定位却不相同。

bootstrap(.yml/.properties)可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。

application(.yml/.properties)可以用来定义应用级别的参数

如果该项目继承注册中心和配置中心,最好声明在bootstrap(.yml/.properties)文件中。
这里可以看看大佬博客:深入理解为什么nacos配置信息要放到bootstrap.properties「源码分析/图文详解」

优先级

在同一级目录下,会先比较前缀bootstrapapplication,其中bootstrapapplication的优先级高,其次再去比较后缀.properties.yml,其中.properties.yml优先级高

所以它们的执行顺序如下:

bootstrap.properties > bootstrap.yml > application.properties > application.yml

配置覆盖

  1. 当同一个配置属性在bootstrap.propertiesbootstrap.yml文件中都存在时,那么properties中的配置会被加载,而忽略yml文件中的配置(即优先级高的配置覆盖优先级低的配置),不同配置相互互补。此处application(.properties/yml)同理
  2. 但当同一配置在bootstrapapplication中都存在时,那么虽然优先加载bootstrap但是会被applicatioin中的配置覆盖,此时则变成了低优先级覆盖高优先的配置,所以网上很多文章所说的高优先级覆盖低优先级其实是不严谨的。
  3. 当同一个配置属性在四种配置(bootstrap.propertiesbootstrap.ymapplication.propertiesapplication.yml)都存在,application.properties里的配置一定是会覆盖其他配置文件,首先遵循第二点:虽然优先加载bootstrap但是会被applicatioin中的配置覆盖,这时候就只剩application.ymlapplication.properties了,此时遵循第一点:即优先级高的配置覆盖优先级低的配置,所以application.propertiest的配置文件会覆盖其他配置文件。

验证覆盖

下面针对覆盖问题进行实例验证:

第一点

验证:当同一个配置属性在bootstrap.properties和bootstrap.yml文件中都存在时,优先级高的配置覆盖优先级低的配置
实例准备:

  • 准备bootstrap.properties和bootstrap.yml文件
  • 采用端口验证,两个配置文件都配置

最后查看是那个端口被采用
bootstrap.properties

server.port=7003

bootstrap.yml

server:port: 7002

最后结果:

  .   ____          _            __ _ _/\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.6)2023-04-20 09:31:52.750  WARN 10044 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[gulimail-coupn] & group[DEFAULT_GROUP]
2023-04-20 09:31:52.758  WARN 10044 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[gulimail-coupn.properties] & group[DEFAULT_GROUP]
2023-04-20 09:31:52.758  INFO 10044 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-gulimail-coupn.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-gulimail-coupn,DEFAULT_GROUP'}]
2023-04-20 09:31:52.761  INFO 10044 --- [           main] com.atguigu.gulimall.coupn.CoupnMain     : No active profile set, falling back to 1 default profile: "default"
2023-04-20 09:31:53.136  INFO 10044 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d68ee34f-7252-38e2-b4f2-94eda3d3bb9d
2023-04-20 09:31:53.303  INFO 10044 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7003 (http)

启动时端口为7003,这里就可以得出结论:配置文件前缀都相同的情况下,优先级高的配置覆盖优先级低的配置

若是无法加载bootstrap文件,添加以下依赖

  <!--添加读取bootstrap配置文件的依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>

第二点

验证:当同一配置在bootstrapapplication中都存在时,优先加载bootstrap但是会被applicatioin中的配置覆盖
实例准备:

  • 准备application.properties和bootstrap.properties文件
  • 采用端口验证,两个配置文件都配置

最后查看是那个端口被采用:
bootstrap.properties

server.port=7003

application.properties

server.port=7001

最后结果:

  .   ____          _            __ _ _/\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.6)2023-04-20 09:49:06.180  WARN 16832 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[gulimail-coupn] & group[DEFAULT_GROUP]
2023-04-20 09:49:06.182  WARN 16832 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[gulimail-coupn.properties] & group[DEFAULT_GROUP]
2023-04-20 09:49:06.183  INFO 16832 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-gulimail-coupn.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-gulimail-coupn,DEFAULT_GROUP'}]
2023-04-20 09:49:06.185  INFO 16832 --- [           main] com.atguigu.gulimall.coupn.CoupnMain     : No active profile set, falling back to 1 default profile: "default"
2023-04-20 09:49:06.549  INFO 16832 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d68ee34f-7252-38e2-b4f2-94eda3d3bb9d
2023-04-20 09:49:06.704  INFO 16832 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7001 (http)

启动时端口为7001,这里就可以得出结论:同一配置在bootstrapapplication中都存在时,优先加载bootstrap但是会被applicatioin中的配置覆盖

第三点

验证:同一个配置属性在四种配置都存在,application.properties里的配置一定是会覆盖其他配置文件
实例准备:

  • 准备application.properties和bootstrap.properties文件
  • 采用端口验证,两个配置文件都配置

最后查看是那个端口被采用:
bootstrap.properties

server.port=7003

bootstrap.yml

server:port: 7002

application.properties

server.port=7001

application.yml

server:port: 7000

最后结果:

 .   ____          _            __ _ _/\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v2.6.6)2023-04-20 09:52:45.868  WARN 9656 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[gulimail-coupn] & group[DEFAULT_GROUP]
2023-04-20 09:52:45.878  WARN 9656 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[gulimail-coupn.properties] & group[DEFAULT_GROUP]
2023-04-20 09:52:45.878  INFO 9656 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-gulimail-coupn.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-gulimail-coupn,DEFAULT_GROUP'}]
2023-04-20 09:52:45.881  INFO 9656 --- [           main] com.atguigu.gulimall.coupn.CoupnMain     : No active profile set, falling back to 1 default profile: "default"
2023-04-20 09:52:46.246  INFO 9656 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d68ee34f-7252-38e2-b4f2-94eda3d3bb9d
2023-04-20 09:52:46.405  INFO 9656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7001 (http)

启动时端口为7001,得出结论:同一个配置属性在四种配置都存在,application.properties里的配置一定是会覆盖其他配置文件

扩展

若你的项目有集成配置中心,那么程序就会优先使用配置中心的配置,配置中心的配置也会覆盖其他配置文件的配置

参考链接:
springBoot项目配置文件加载优先级及同配置覆盖问题

平阳教育网