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

相关推荐
鱼宵23 分钟前
LangChain4j 结构化输出:让模型吐出 Java 对象,JSON 不再手写解析
java·开发语言·json·langchain4j
HSunR36 分钟前
ruoyi 若依 自定义注解 参数校验
java·前端·数据库
Wang's Blog1 小时前
Java 项目部署之 Docker工具快速入门: Docker 是什么以及它如何解决部署环境问题
java·开发语言·docker
老木避暑研究所1 小时前
从原始数据到洞察:一次完整的避暑房环境数据采集、清洗与可视化分析
java·开发语言
前端 贾公子1 小时前
LangGraph == 图的状态(State)管理 (上)
java·开发语言·数据库
huaweichenai2 小时前
spring boot 实现file文件上传
java·spring boot·后端
VX_bysjlw9852 小时前
数码设备销售网站设计与实现39138-计算机毕设原创(免费领源码+带部署教程)
java·vue.js·spring boot·mysql·tomcat·mybatis·idea
荣码2 小时前
从0到1搭一个生产级RAG系统:串联前面21篇所有知识
java·python
xuxigifxfh2 小时前
牛客:HJ170 01序列
java·开发语言·算法·华为机考