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 文件优先。

相关推荐
焰火19998 分钟前
[Java]自定义重试工具类
java
SuperherRo1 小时前
JAVA攻防-Shiro专题&断点调试&有key利用链&URL&CC&CB&原生反序列化&加密逻辑
java·shiro·反序列化·有key·利用链·原生反序列化·加密逻辑
桦说编程1 小时前
简单方法实现子任务耗时统计
java·后端·监控
爱笑的眼睛111 小时前
超越可视化:降维算法组件的深度解析与工程实践
java·人工智能·python·ai
BD_Marathon1 小时前
配置文件分类
spring boot
M***Z2101 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
盖世英雄酱581362 小时前
物品超领取损失1万事故复盘(一)
java·后端
CryptoRzz2 小时前
印度尼西亚(IDX)股票数据对接开发
java·后端·websocket·web3·区块链
f***28142 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins