SpringBoot: 配置加載順序
SpringBoot: 配置加载顺序
文章出处:原文地址
在应用程序中各种配置是不可避免的,Spring中对配置的抽象(Environment)可以说是达到了极致,其中有一项尤为突出:PropertySource(配置来源),配置来源通常包括命令行参数,系统属性,系统变量,perperties文件等。在使用SpringBoot过程中,将这些技术更进一步发挥,已经可以在很多环境下使用各种各样的配置。
配置的使用通常使用${}占位符来表示,可以在 properties文件中,基于XML的applicationContext.xml配置中,基于Annotation的@Value、@ConfigurationProperies中使用。
1、查找顺序
在使用过程中,查找顺序配置项的如何呢?
Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). 用于开发环境。@TestPropertySource
annotations on your tests. (用于单元测试环境)@SpringBootTest#properties
annotation attribute on your tests.(用于单元测试环境)Command line arguments. (命令行参数 java -jar xxx.jar args, 其中参数形式是 --key1=value1 --key2=value2)
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 only has properties inrandom.*
. (这个是用于取一个随机数,例如 random.int, random.long, random.uuid, random.int(10), random.int[10,100]等等)Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants) 其实包括 .properties,.xml,.yml,yaml,详情见下面说明。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.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; }
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質文章