本文主要介绍 Spring Boot 配置加载方式及配置属性加载顺序,内容基于 Spring Boot 2.x 进行详解。

阅读文章的过程中如果有任何疑问,欢迎添加笔者为好友,拉您进【七日书摘】微信交流群,一起交流技术,一起打造高质量的职场技术交流圈子,抱团取暖,共同进步。
七日书摘官方群.jpg

Spring Boot 加载配置方式如下

  1. properties文件;
  2. YAML文件;
  3. 系统环境变量;
  4. 命令行参数;

等等……

也可以在 Spring Beans 里面直接使用这些配置文件中加载的值,如:

  1. 使用 @Value 注解直接注入对应的值,这能获取到 Spring 中 Environment 的值;
  2. 使用 @ConfigurationProperties 注解把对应的值绑定到一个对象;
  3. 直接获取注入 Environment 进行获取;
  4. 使用 @PropertySource 注入属性的值;

除此之外,还可以使用 @PropertySource配合@Value@ConfigurationProperties配合@PropertySource配合@Value@ConfigurationProperties配合@Value 实现属性的加载注入。

Spring Boot 配置文件加载位置

在 Spring Boot 启动过程中会自动扫描如下位置的 application.properties 或者 application.yml 文件作为 Spring Boot 的默认配置文件。

1、project:/config/ 
    项目根目录下面 config 文件夹里的配置文件
2、project:/
    项目根目录下面的配置文件
3、classpath:/config/
    Resources文件夹下面config文件夹里的配置文件
4、classpath:/
    Resources文件夹下面的配置文件

Spring Boot 项目启动时会按照以上顺序由高到低读取配置,高优先级的属性会覆盖低优先级的属性,属性文件之间存在互补配置的特性。

当然也可以在 Spring Boot 可运行 jar 文件启动时通过 spring.config.location 来指定默认配置文件的位置,如:
-Dspring.config.location=D:/application.properties 来改变以上默认顺序。

配置属性加载顺序如下

1、Devtools global settings properties in the $HOME/.config/spring-boot folder when devtools is active.
    开发者工具 `Devtools` 全局配置参数;

2、@TestPropertySource annotations on your tests.
    单元测试上的 `@TestPropertySource` 注解指定的参数;

3、properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
    单元测试上的 `@SpringBootTest` 注解指定的参数;

4、Command line arguments.
    命令行指定的参数,如 `java -jar springboot.jar --website="http://qirishuzhai.com"`;

5、Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
    命令行中的 `SPRING_APPLICATION_JSONJSON` 指定参数, 如 `java -Dspring.application.json='{"website":"http://qirishuzhai.com"}' -jar springboot.jar`

6、ServletConfig init parameters.
    `ServletConfig` 初始化参数;

7、ServletContext init parameters.
    `ServletContext` 初始化参数;

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

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

10、OS environment variables.
    操作系统环境变量参数;

11、A RandomValuePropertySource that has properties only in random.*.
    `RandomValuePropertySource` 随机数,仅匹配:`ramdom.*`;

12、Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
    JAR包外面的配置文件参数(`application-{profile}.properties(YAML)`)

13、Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
    JAR包里面的配置文件参数(`application-{profile}.properties(YAML)`)

14、Application properties outside of your packaged jar (application.properties and YAML variants).
    JAR包外面的配置文件参数(`application.properties(YAML)`)

15、Application properties packaged inside your jar (application.properties and YAML variants).
    JAR包里面的配置文件参数(`application.properties(YAML)`)

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.
    `@Configuration`配置文件上 `@PropertySource` 注解加载的参数;

17、Default properties (specified by setting SpringApplication.setDefaultProperties).
    默认参数(通过 `SpringApplication.setDefaultProperties` 设定);

详见:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config

注意:数字小的优先级越高,即数字小的会覆盖数字大的参数值,我们来实践下,验证以上配置参数的加载顺序。

Spring Boot 属性加载顺序

优先级按照顺序由高到低,数字越小优先级越高

  1. 在命令行中传入的参数,类似于 java -jar -Denv=DEV 之类。
  2. SPRING_APPLICATION_JSON 属性,该属性以 JSON 形式存储在系统环境变量中。
  3. java:comp/env 中 JNDI 属性。
  4. Java 的系统属性,可通过 System.getProperties() 获得相关内容。
  5. 操作系统中的环境变量。
  6. 通过 random.* 配置的随机属性。
  7. 位于当前应用 jar 包外,针对不同 {profile} 环境的配置文件内容。
  8. 位于当前应用 jar 包内,针对不同 {profile} 环境的配置文件内容。
  9. 位于当前应用 jar 包外的 application.propertiesapplication.yml 配置内容。
  10. 位于当前应用 jar 包内的 application.propertiesapplication.yml 配置内容。
  11. @Configuration 注解修改的类中,通过 @PropertySource 注解定义的属性。
  12. 应用默认属性,使用SpringApplication.setDefaultProperties 定义的属性内容。

注意:当发现自己使用 Spring Boot 属性时,如果使用到的属性与自己希望得到的属性值存在差异时,请从以上加载顺序中去查看相关属性内容。

------完------

更多学习讨论欢迎进入七日书摘官方群: 七日书摘官方群

七日书摘官方群群聊二维码.png

参考资源:
https://blog.csdn.net/youanyyou/article/details/82107780
https://my.oschina.net/imlim/blog/1859091