spring boot 配置加载顺序

由官网的文档得知

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

  1. Default properties (specified by setting SpringApplication.setDefaultProperties).
  2. @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
  3. Config data (such as application.properties files).
  4. A RandomValuePropertySource that has properties only in random.*.
  5. OS environment variables.
  6. Java System properties (System.getProperties()).
  7. JNDI attributes from java:comp/env.
  8. ServletContext init parameters.
  9. ServletConfig init parameters.
  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  11. Command line arguments.
  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  13. @DynamicPropertySource annotations in your tests.
  14. @TestPropertySource annotations on your tests.
  15. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

从第3步开始,加载 properties 文件中的配置,接下来第5步加载操作系统环境变量,第6步加载系统属性,第11步加载命令行参数。

如果同一个参数在前面声明了,后面也声明了同样的参数,后面的会覆盖前面的,即以后面的为准。

It is recommended to stick with one format for your entire application. If you have configuration files with both .properties and YAML format in the same location, .properties takes precedence.

如果 properties 和 yaml 文件都存在,properties 文件优先。

相关推荐
秋千码途3 分钟前
小架构step系列08:logback.xml的配置
xml·java·logback
飞翔的佩奇5 分钟前
Java项目:基于SSM框架实现的旅游协会管理系统【ssm+B/S架构+源码+数据库+毕业论文】
java·数据库·mysql·毕业设计·ssm·旅游·jsp
时来天地皆同力.24 分钟前
Java面试基础:概念
java·开发语言·jvm
找不到、了1 小时前
Spring的Bean原型模式下的使用
java·spring·原型模式
阿华的代码王国1 小时前
【Android】搭配安卓环境及设备连接
android·java
YuTaoShao1 小时前
【LeetCode 热题 100】141. 环形链表——快慢指针
java·算法·leetcode·链表
铲子Zzz2 小时前
Java使用接口AES进行加密+微信小程序接收解密
java·开发语言·微信小程序
霖檬ing2 小时前
K8s——配置管理(1)
java·贪心算法·kubernetes
Vic101013 小时前
Java 开发笔记:多线程查询逻辑的抽象与优化
java·服务器·笔记
Biaobiaone3 小时前
Java中的生产消费模型解析
java·开发语言