2

springboot(13)配置读取优先级

 2 years ago
source link: https://wakzz.cn/2018/01/23/springboot/(13)%E9%85%8D%E7%BD%AE%E8%AF%BB%E5%8F%96%E4%BC%98%E5%85%88%E7%BA%A7/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

springboot(13)配置读取优先级

祈雨的博客
2018-01-23

Spring Boot允许将配置外部化(externalize),这样你就能够在不同的环境下使用相同的代码。你可以使用properties文件,YAML文件,环境变量和命令行参数来外部化配置。使用@Value注解,可以直接将属性值注入到beans中,然后通过Spring的 Environment 抽象或通过 @ConfigurationProperties 绑定到结构化对象来访问。

Spring Boot设计了一个非常特别的 PropertySource 顺序,以允许对属性值进行合理的覆盖,属性会以如下的顺序进行设值:
|读取顺序 |来源|
|—–|:—–:|
|1 |home目录下的devtools全局设置属性( ~/.spring-bootdevtools.properties ,如果devtools激活)|
|2 |测试用例上的@TestPropertySource注解|
|3 |测试用例上的@SpringBootTest#properties注解|
|4 |命令行参数|
|5 |来自 SPRING_APPLICATION_JSON 的属性(环境变量或系统属性中内嵌的内联JSON)|
|6 |ServletConfig 初始化参数|
|7 |ServletContext 初始化参数|
|8 |来自于 java:comp/env 的JNDI属性|
|9 |Java系统属性(System.getProperties())|
|10 |操作系统环境变量|
|11 |RandomValuePropertySource,只包含 random.* 中的属性|
|12 |没有打进jar包的Profile-specific应用属性( application-{profile}.properties 和YAML变量)|
|13 |打进jar包中的Profile-specific应用属性( application-{profile}.properties 和YAML变量)|
|14 |没有打进jar包的应用配置( application.properties 和YAML变量)|
|15 |打进jar包中的应用配置( application.properties 和YAML变量)|
|16 |@Configuration 类上的 @PropertySource 注解|
|17 |默认属性(使用 SpringApplication.setDefaultProperties 指定)|


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK