栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot: 配置加载顺序

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot: 配置加载顺序

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. 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;
}


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号