SpringBoot配置文件你了解多少?
環(huán)境:springboot2.2.13
SpringBoot 中有以下兩種配置文件
- bootstrap (.yml 或者 .properties)
- application (.yml 或者 .properties)
接下來說下這兩個配置文件有什么區(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)用程序的父上下文,也就是說 bootstrap 加載優(yōu)先于 applicaton。bootstrap 主要用于從額外的資源來加載配置信息,還可以在本地外部配置文件中解密屬性。這兩個上下文共用一個環(huán)境,它是任何Spring應(yīng)用程序的外部屬性的來源。bootstrap 里面的屬性會優(yōu)先加載,它們默認也不能被本地相同配置覆蓋也就是說bootstrap中的配置不能被覆蓋。
bootstrap/ application 的應(yīng)用場景
application 配置文件這個容易理解,主要用于 Spring Boot 項目的自動化配置。
bootstrap 配置文件有以下幾個應(yīng)用場景。
- 使用 Spring Cloud Config 配置中心時,這時需要在 bootstrap 配置文件中添加連接到配置中心的配置屬性來加載外部配置中心的配置信息;
- 一些固定的不能被覆蓋的屬性
- 一些加密/解密的場景;
以下是來自官方對bootstrap.[yml/properties]的一個說明:
- 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兩個配置文件必須是spring的配置文件。
配置文件默認值
有如下代碼:
- @Value("${pack.name}")
- private String name ;
當(dāng)配置文件中沒有配置pack.name時,這時候服務(wù)啟動會報錯,這時候我們可以通過設(shè)置默認值來防止錯誤。
- @Value("${pack.name:xx}")
- private String name ;
這里冒號 “:” 后面就是設(shè)置的默認值,這里也可以什么也不寫${pack.name:}。
加載制定環(huán)境的配置文件
一般我們都會為測試環(huán)境,開發(fā)環(huán)境,生產(chǎn)環(huán)境分別設(shè)置不同的配置文件,不同的環(huán)境加載不同的配置文件。
現(xiàn)有如下配置文件:
生成環(huán)境加載prod文件,測試環(huán)境加載test文件,開發(fā)環(huán)境加載dev文件,只需在application.yml配置文件中加入如下配置。
- spring:
- profiles:
- active:
- - dev
這是在配置文件中寫死,我們也可以在命令行制定
- java -jar xxxxx.jar --spring.profiles.active=dev
通過include包含多個配置文件,include是與環(huán)境無關(guān)的(也就是不管是什么環(huán)境都會加載)
- spring:
- profiles:
- active:
- - dev
- include:
- - cloud
- - secret
配置文件加載順序
查看
ConfigFileApplicationListener.java源碼,該文件中定義了從哪些地方加載配置文件。
加載順序:
- file:./config/
- file:./config/*/
- file:./
- classpath:config/
- classpath:
優(yōu)先級從高到低,高的覆蓋低的。
默認情況下加載的是application.yml或application.properties文件。
在啟動應(yīng)用程序的時候可以制定配置文件的名稱
- java -jar xxxxx.jar --spring.config.name=myapp
源碼:
通過代碼讀取配置文件
- @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() ;
- }
- }
- }
這里通過PropertiesLoaderUtils工具類來加載資源
完畢!!!