SpringBoot引入額外的YAML配置文件的代碼實現(xiàn)
背景
在SpringBoot項目中,有時需要引入除application.yml之外的配置文件(例如在開發(fā)公共組件時)。使用@PropertySource注解可以實現(xiàn)這一需求,但有一些細節(jié)點需要注意,在此記錄。
代碼實現(xiàn)
假設(shè)有一份名為extra.yml的配置文件:
# extra.yml extra: name: 張三 order: 3
對應(yīng)的配置bean為:
@Data
@ConfigurationProperties("extra")
public class ExtraProperties {
private String name;
private Integer order;
}
在配置類上添加相關(guān)注解,將extra.yml配置文件添加到Spring環(huán)境中:
@Configuration
@EnableConfigurationProperties(ExtraProperties.class)
@PropertySource(
// 配置文件路徑
value = "classpath:/extra.yml",
// 當配置文件不存在時,是忽略還是報錯
ignoreResourceNotFound = true,
// 配置文件編碼
encoding = "UTF-8",
// 配置文件加載工廠
factory = YamlPropertySourceFactory.class)
public class ExtraConfig {
}
由于@PropertySource默認支持的是.properties格式的配置文件,而我們一般使用的是YAML格式的,因此這里自定義了配置文件加載工廠,支持YAML,并解決ignoreResourceNotFound不生效的問題:
/**
* YAML配置文件加載工廠
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
try {
return new YamlPropertySourceLoader()
.load(resource.getResource().getFilename(), resource.getResource())
.get(0);
} catch (IllegalStateException e) {
// 如果YAML配置文件不存在,希望能忽略該文件,而不是引發(fā)異常導(dǎo)致Spring容器啟動失敗
// 需要往外拋FileNotFoundException,Spring捕捉到后會忽略該異常(當 ignoreResourceNotFound = true 時)
if (e.getCause() instanceof FileNotFoundException) {
throw (FileNotFoundException) e.getCause();
} else {
throw e;
}
}
}
}
這樣,ExtraProperties配置bean里的屬性值, 就與extra.yml里的配置值綁定在一起了。
補充說明
標準配置文件application.yml的生效優(yōu)先級高于額外引入的配置文件。如果application.yml中指定了相同的配置項,則它會覆蓋extra.yml中對應(yīng)的配置項:
# application.yml,會覆蓋extra.yml中的相同配置項 extra: name: 李四 order: 4
當然,如果使用了環(huán)境配置文件application-{profile}.yml,則它的生效優(yōu)先級又會高于application.yml。
另外,@PropertySource支持引入多個配置文件。例如,在引入extra.yml的同時,引入對應(yīng)的環(huán)境配置文件extra-{profile}.yml:
@Configuration
@EnableConfigurationProperties(ExtraProperties.class)
@PropertySource(
value = {"classpath:/extra.yml", "classpath:/extra-${spring.profiles.active}.yml"},
ignoreResourceNotFound = true,
encoding = "UTF-8",
// 配置文件加載工廠
factory = YamlPropertySourceFactory.class)
public class ExtraConfig {
}
這里,Spring會將占位符${spring.profiles.active}解析為對應(yīng)的值。例如,在application.yml中指定spring.profiles.active=dev,那么配置文件extra-dev.yml會被引入(如有),它的生效優(yōu)先級高于extra.yml,但低于application.yml。
# extra-dev.yml,會覆蓋extra.yml中的相同配置項 extra: name: 王五 order: 5
總結(jié)
- @PropertySource用于引入額外的配置文件。
- 通過自定義配置文件加載工廠,可支持YAML文件解析,并支持ignoreResourceNotFound。
- 配置文件生效的優(yōu)先級順序為:application-{profile}.yml>application.yml>extra-{profile}.yml>extra.yml。
以上就是SpringBoot引入額外的YAML配置文件的代碼實現(xiàn)的詳細內(nèi)容,更多關(guān)于SpringBoot引入額外YAML文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用MyBatis-Plus靈活處理JSON字段的技巧與最佳實踐
這篇文章主要給大家介紹了關(guān)于利用MyBatis-Plus靈活處理JSON字段的技巧與最佳實踐,Mybatis-Plus可以很方便地處理JSON字段,在實體類中可以使用@JSONField注解來標記JSON字段,需要的朋友可以參考下2024-07-07
Spring Boot 配置和使用多線程池的實現(xiàn)
這篇文章主要介紹了Spring Boot 配置和使用多線程池的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Spring?Cloud?Stream實現(xiàn)數(shù)據(jù)流處理
Spring?Cloud?Stream的核心是Stream,準確來講Spring?Cloud?Stream提供了一整套數(shù)據(jù)流走向(流向)的API,?它的最終目的是使我們不關(guān)心數(shù)據(jù)的流入和寫出,而只關(guān)心對數(shù)據(jù)的業(yè)務(wù)處理,本文給大家介紹了Spring?Cloud?Stream實現(xiàn)數(shù)據(jù)流處理,需要的朋友可以參考下2024-11-11
Springboot攔截器如何獲取@RequestBody參數(shù)
這篇文章主要介紹了Springboot攔截器如何獲取@RequestBody參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

