由官网的文档得知
https://docs.spring.io/spring-boot/docs/3.0.0/reference/html/features.html#features.external-config
- Default properties (specified by setting SpringApplication.setDefaultProperties).
- @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.
- Config data (such as application.properties files).
- A RandomValuePropertySource that has properties only in random.*.
- OS environment variables.
- Java System properties (System.getProperties()).
- JNDI attributes from java:comp/env.
- ServletContext init parameters.
- ServletConfig init parameters.
- Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
- Command line arguments.
- properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
- @DynamicPropertySource annotations in your tests.
- @TestPropertySource annotations on your tests.
- 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 文件优先。
https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config
从 spring boot 2.4.0 开始,官网文档位置发生了改变。
https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/spring-boot-features.html#boot-features-external-config
https://docs.spring.io/spring-boot/docs/2.5.0/reference/html/features.html#features.external-config