微信公众号:路人zhang
扫码关注微信公众号

回复“面试手册”,获取本站PDF版

回复“简历”,获取高质量简历模板

回复“加群”,加入程序员交流群

回复“电子书”,获取程序员类电子书

当前位置: Java > Spring高频面试题 > 37.SpringBoot配置加载顺序

关于这个问题,官网有着比较详细的说明,https://docs.spring.io/spring-boot/docs/2.1.11.RELEASE/reference/html/boot-features-external-config.html

简单总结下

情况1

当同目录同时存在application.properties和application.yml时,哪个优先级会更高呢?我试了以下

结论是application.properties,也就是说两个文件存在相同配置,application.properties会覆盖application.yml

情况2

当在这四个位置同时存在配置文件,它们的优先级是什么样的呢?

结论是1423,官方原文优先级如下:

  1. A /config subdirectory of the current directory(位置1)
  2. The current directory(位置4)
  3. A classpath /config package(位置2)
  4. The classpath root(位置3)

就是位置1的配置文件的优先级最高

情况3

外部配置优先级,从高到底,官方原文如下:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that has properties only in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @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.
  17. Default properties (specified by setting SpringApplication.setDefaultProperties).

在网上找了个翻译过的,如下

1、开发者工具 Devtools 全局配置参数;

2、单元测试上的 @TestPropertySource 注解指定的参数;

3、单元测试上的 @SpringBootTest 注解指定的参数;

4、命令行指定的参数,如 java -jar springboot.jar --name="Java技术栈"

5、命令行中的 SPRING_APPLICATION_JSONJSON 指定参数, 如 java -Dspring.application.json='{"name":"Java技术栈"}' -jar springboot.jar

6、ServletConfig 初始化参数;

7、ServletContext 初始化参数;

8、JNDI参数(如 java:comp/env/spring.application.json);

9、Java系统参数(来源:System.getProperties());

10、操作系统环境变量参数;

11、RandomValuePropertySource 随机数,仅匹配:ramdom.*

12、JAR包外面的配置文件参数(application-{profile}.properties(YAML)

13、JAR包里面的配置文件参数(application-{profile}.properties(YAML)

14、JAR包外面的配置文件参数(application.properties(YAML)

15、JAR包里面的配置文件参数(application.properties(YAML)

16、@Configuration配置文件上 @PropertySource 注解加载的参数;

17、默认参数(通过 SpringApplication.setDefaultProperties 指定);

本站链接:https://www.mianshi.online如需勘误或投稿,请联系微信:lurenzhang888


点击面试手册,获取本站面试手册PDF完整版