第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

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

复制代码


點擊查看更多內容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質文章

正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消