1.为阅读体验,本站无任何广告,也无任何盈利方法,站长一直在用爱发电,现濒临倒闭,希望有能力的同学能帮忙分担服务器成本
2.捐助10元及以上同学,可添加站长微信lurenzhang888,备注捐助,网站倒闭后可联系站长领取本站pdf内容
3.若网站能存活下来,后续将会持续更新内容
关于这个问题,官网有着比较详细的说明,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,官方原文优先级如下:
- A
/configsubdirectory of the current directory(位置1) - The current directory(位置4)
- A classpath
/configpackage(位置2) - The classpath root(位置3)
就是位置1的配置文件的优先级最高
情况3
外部配置优先级,从高到底,官方原文如下:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.propertieswhen devtools is active). @TestPropertySourceannotations on your tests.propertiesattribute on your tests. Available on@SpringBootTestand the test annotations for testing a particular slice of your application.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON(inline JSON embedded in an environment variable or system property). ServletConfiginit parameters.ServletContextinit parameters.- JNDI attributes from
java:comp/env. - Java System properties (
System.getProperties()). - OS environment variables.
- A
RandomValuePropertySourcethat has properties only inrandom.*. - Profile-specific application properties outside of your packaged jar (
application-{profile}.propertiesand YAML variants). - Profile-specific application properties packaged inside your jar (
application-{profile}.propertiesand YAML variants). - Application properties outside of your packaged jar (
application.propertiesand YAML variants). - Application properties packaged inside your jar (
application.propertiesand YAML variants). @PropertySourceannotations on your@Configurationclasses. Please note that such property sources are not added to theEnvironmentuntil the application context is being refreshed. This is too late to configure certain properties such aslogging.*andspring.main.*which are read before refresh begins.- 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完整版