ITPub博客

首页 > IT职业 > IT图书 > SpringBoot: 配置加载顺序

SpringBoot: 配置加载顺序

IT图书 作者:wanxud 时间:2021-09-09 09:11:26 0 删除 编辑

SpringBoot: 配置加载顺序

    文章出处:原文地址    

       在应用程序中各种配置是不可避免的,Spring中对配置的抽象(Environment)可以说是达到了极致,其中有一项尤为突出:PropertySource(配置来源),配置来源通常包括命令行参数,系统属性,系统变量,perperties文件等。在使用SpringBoot过程中,将这些技术更进一步发挥,已经可以在很多环境下使用各种各样的配置。

配置的使用通常使用${}占位符来表示,可以在 properties文件中,基于XML的applicationContext.xml配置中,基于Annotation的@Value、@ConfigurationProperies中使用。

1、查找顺序

在使用过程中,查找顺序配置项的如何呢?

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).  用于开发环境。

  2. @TestPropertySource annotations on your tests.         (用于单元测试环境)

  3. @SpringBootTest#properties annotation attribute on your tests.(用于单元测试环境)

  4. Command line arguments.  (命令行参数 java -jar xxx.jar args, 其中参数形式是 --key1=value1 --key2=value2)

  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 only has properties in random.*. (这个是用于取一个随机数,例如 random.int, random.long, random.uuid, random.int(10), random.int[10,100]等等)

  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants) 其实包括 .properties,.xml,.yml,yaml,详情见下面说明。

  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.

  17. Default properties (specified using SpringApplication.setDefaultProperties).

对于第12, 13, 14, 15 项,详细情况是这样的:

在程序启动是,会查找并加载Profiles文件,它的详细过程是:

1) 取得要激活哪些profiles,以及这些profile可能存放的位置。
1.1)  使用PropertySources.get("spring.profiles.active")获取到要激活哪些Profiles,如果不指定会使用默认的名字:application。
当然了,也可以使用编程的方式设置: 在程序开始之前执行代码:SpringApplication.setAdditionalProfiles("yourProfile")
1.2)  使用PropertySources.get("spring.config.location")获取要激活的profiles可能存放的位置。如果不指定该属性,会使用默认位置:

file:./config/
file:./
classpath:./config/
classpath:./

2) 从"spring.config.location指定的位置去加载"spring.profiles.active指定的配置文件。

 配置文件以 [.properties, .xml, .yml, yaml]顺序查找,找不到的情况下会找下一个。

examples:
如果没有配置spring.profiles.active, spring.config.location, 则会找

图片描述

file:./config/application-default.[peroperties|xml|yml|yaml]
file:./application-default.[peroperties|xml|yml|yaml]
classpath:./config/application-default.[peroperties|xml|yml|yaml]
classpath:./application-default.[peroperties|xml|yml|yaml]

file:./config/application.[peroperties|xml|yml|yaml]
file:./application.[peroperties|xml|yml|yaml]
classpath:./config/application.[peroperties|xml|yml|yaml]
classpath:./application.[peroperties|xml|yml|yaml]

图片描述

 

2、示例

一个YAML格式的profile:

图片描述

spring:
 profiles: development
db:
 url: jdbc:hsqldb:file:testdb
 username: sa
 password:
---
spring:
 profiles: test
db:
 url: jdbc:mysql://localhost/test
 username: test
 password: test

图片描述

下面使用@Value使用的列子,配置需要在上述搜索路径下:

图片描述

@Configurationpublic class DBSettings {
@Value("${db.url}") private String url;
@Value("${db.username}") private String username;
@Value("${db.password}") private String password;
}

图片描述

 

下面是@ConfigurationProperties例子 :

图片描述

@Component
@ConfigurationProperties(prefix="db")public class DBSettings { private String url; private String username; private String password;
}

图片描述

下面使用@PropertySource 结合 @Value的例子,这种情况一般用于非上述搜索路径下的配置:

图片描述

@Component
@Configuration
@PropertySource("classpath:/myconfig/myconfig.yaml")public class DBSettings {
@Value("${db.url}") private String url;
@Value("${db.username}") private String username;
@Value("${db.password}") private String password;
}

图片描述


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3451/viewspace-2808495/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    100
  • 访问量
    1145