SpringBoot配置文件你了解多少?
環(huán)境:springboot2.2.13
SpringBoot 中有以下兩種配置文件
- bootstrap (.yml 或者 .properties)
- application (.yml 或者 .properties)
接下來(lái)說(shuō)下這兩個(gè)配置文件有什么區(qū)別!
bootstrap/ application 的區(qū)別
bootstrap.yml(bootstrap.properties)先加載
application.yml(application.properties)后加載
bootstrap.yml 用于應(yīng)用程序上下文的引導(dǎo)階段,由父Spring ApplicationContext加載。父ApplicationContext 被加載到使用application.yml的之前。
在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另外一種是 application, bootstrap 是應(yīng)用程序的父上下文,也就是說(shuō) bootstrap 加載優(yōu)先于 applicaton。bootstrap 主要用于從額外的資源來(lái)加載配置信息,還可以在本地外部配置文件中解密屬性。這兩個(gè)上下文共用一個(gè)環(huán)境,它是任何Spring應(yīng)用程序的外部屬性的來(lái)源。bootstrap 里面的屬性會(huì)優(yōu)先加載,它們默認(rèn)也不能被本地相同配置覆蓋也就是說(shuō)bootstrap中的配置不能被覆蓋。
bootstrap/ application 的應(yīng)用場(chǎng)景
application 配置文件這個(gè)容易理解,主要用于 Spring Boot 項(xiàng)目的自動(dòng)化配置。
bootstrap 配置文件有以下幾個(gè)應(yīng)用場(chǎng)景。
- 使用 Spring Cloud Config 配置中心時(shí),這時(shí)需要在 bootstrap 配置文件中添加連接到配置中心的配置屬性來(lái)加載外部配置中心的配置信息;
- 一些固定的不能被覆蓋的屬性
- 一些加密/解密的場(chǎng)景;
以下是來(lái)自官方對(duì)bootstrap.[yml/properties]的一個(gè)說(shuō)明:
- A Spring Cloud application operates by creating a “bootstrap” context, which is a parent context for the main application. This context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. The two contexts share an Environment, which is the source of external properties for any Spring application. By default, bootstrap properties (not bootstrap.properties but properties that are loaded during the bootstrap phase) are added with high precedence, so they cannot be overridden by local configuration.
- The bootstrap context uses a different convention for locating external configuration than the main application context. Instead of application.yml (or .properties), you can use bootstrap.yml, keeping the external configuration for bootstrap and main context nicely separate
自定義配置文件
- @Configuration
- @ConfigurationProperties(prefix = "pack")
- @PropertySource(value = "classpath:config.properties")
- public class PackProperties {
- private String name;
- private Integer age ;
- }
- pack.name=xxxx
- pack.age=10
注意:@PropertySource只能加載properties文件不能加載yml文件。
導(dǎo)入Spring XML文件
- @Configuration
- @ImportResource(locations = {"classpath:application-jdbc.xml", "classpath:application-aop.xml"})
- public class ResourceConfig {
- }
application-jdbc.xml,application-aop.xml兩個(gè)配置文件必須是spring的配置文件。
配置文件默認(rèn)值
有如下代碼:
- @Value("${pack.name}")
- private String name ;
當(dāng)配置文件中沒有配置pack.name時(shí),這時(shí)候服務(wù)啟動(dòng)會(huì)報(bào)錯(cuò),這時(shí)候我們可以通過(guò)設(shè)置默認(rèn)值來(lái)防止錯(cuò)誤。
- @Value("${pack.name:xx}")
- private String name ;
這里冒號(hào) “:” 后面就是設(shè)置的默認(rèn)值,這里也可以什么也不寫${pack.name:}。
加載制定環(huán)境的配置文件
一般我們都會(huì)為測(cè)試環(huán)境,開發(fā)環(huán)境,生產(chǎn)環(huán)境分別設(shè)置不同的配置文件,不同的環(huán)境加載不同的配置文件。
現(xiàn)有如下配置文件:
生成環(huán)境加載prod文件,測(cè)試環(huán)境加載test文件,開發(fā)環(huán)境加載dev文件,只需在application.yml配置文件中加入如下配置。
- spring:
- profiles:
- active:
- - dev
這是在配置文件中寫死,我們也可以在命令行制定
- java -jar xxxxx.jar --spring.profiles.active=dev
通過(guò)include包含多個(gè)配置文件,include是與環(huán)境無(wú)關(guān)的(也就是不管是什么環(huán)境都會(huì)加載)
- spring:
- profiles:
- active:
- - dev
- include:
- - cloud
- - secret
配置文件加載順序
查看
ConfigFileApplicationListener.java源碼,該文件中定義了從哪些地方加載配置文件。
加載順序:
- file:./config/
- file:./config/*/
- file:./
- classpath:config/
- classpath:
優(yōu)先級(jí)從高到低,高的覆蓋低的。
默認(rèn)情況下加載的是application.yml或application.properties文件。
在啟動(dòng)應(yīng)用程序的時(shí)候可以制定配置文件的名稱
- java -jar xxxxx.jar --spring.config.name=myapp
源碼:
通過(guò)代碼讀取配置文件
- @SpringBootApplication
- public class SpringBootJwtApplication implements ApplicationRunner{
- public static void main(String[] args) {
- SpringApplication.run(SpringBootJwtApplication.class, args);
- }
- @Override
- public void run(ApplicationArguments args) throws Exception {
- ClassPathResource resource = new ClassPathResource("config.properties");
- try {
- Properties properties = PropertiesLoaderUtils.loadProperties(resource);
- System.out.println(properties) ;
- } catch (IOException e) {
- e.printStackTrace() ;
- }
- }
- }
這里通過(guò)PropertiesLoaderUtils工具類來(lái)加載資源
完畢!!!