回复“面试手册”,获取本站PDF版
回复“简历”,获取高质量简历模板
回复“加群”,加入程序员交流群
回复“电子书”,获取程序员类电子书
关于这个问题,官网有着比较详细的说明,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
/config
subdirectory of the current directory(位置1) - The current directory(位置4)
- A classpath
/config
package(位置2) - The classpath root(位置3)
就是位置1的配置文件的优先级最高
情况3
外部配置优先级,从高到底,官方原文如下:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). @TestPropertySource
annotations on your tests.properties
attribute on your tests. Available on@SpringBootTest
and 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). ServletConfig
init parameters.ServletContext
init parameters.- JNDI attributes from
java:comp/env
. - Java System properties (
System.getProperties()
). - OS environment variables.
- A
RandomValuePropertySource
that has properties only inrandom.*
. - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants). - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants). - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). @PropertySource
annotations on your@Configuration
classes. Please note that such property sources are not added to theEnvironment
until 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完整版