SpringBoot引入額外的YAML配置文件的代碼實(shí)現(xiàn)
背景
在SpringBoot項(xiàng)目中,有時(shí)需要引入除application.yml
之外的配置文件(例如在開(kāi)發(fā)公共組件時(shí))。使用@PropertySource
注解可以實(shí)現(xiàn)這一需求,但有一些細(xì)節(jié)點(diǎn)需要注意,在此記錄。
代碼實(shí)現(xiàn)
假設(shè)有一份名為extra.yml
的配置文件:
# extra.yml extra: name: 張三 order: 3
對(duì)應(yīng)的配置bean為:
@Data @ConfigurationProperties("extra") public class ExtraProperties { private String name; private Integer order; }
在配置類(lèi)上添加相關(guān)注解,將extra.yml
配置文件添加到Spring環(huán)境中:
@Configuration @EnableConfigurationProperties(ExtraProperties.class) @PropertySource( // 配置文件路徑 value = "classpath:/extra.yml", // 當(dāng)配置文件不存在時(shí),是忽略還是報(bào)錯(cuò) ignoreResourceNotFound = true, // 配置文件編碼 encoding = "UTF-8", // 配置文件加載工廠(chǎng) factory = YamlPropertySourceFactory.class) public class ExtraConfig { }
由于@PropertySource
默認(rèn)支持的是.properties
格式的配置文件,而我們一般使用的是YAML格式的,因此這里自定義了配置文件加載工廠(chǎng),支持YAML,并解決ignoreResourceNotFound
不生效的問(wèn)題:
/** * YAML配置文件加載工廠(chǎng) */ 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容器啟動(dòng)失敗 // 需要往外拋FileNotFoundException,Spring捕捉到后會(huì)忽略該異常(當(dāng) ignoreResourceNotFound = true 時(shí)) if (e.getCause() instanceof FileNotFoundException) { throw (FileNotFoundException) e.getCause(); } else { throw e; } } } }
這樣,ExtraProperties配置bean里的屬性值, 就與extra.yml里的配置值綁定在一起了。
補(bǔ)充說(shuō)明
標(biāo)準(zhǔn)配置文件application.yml的生效優(yōu)先級(jí)高于額外引入的配置文件。如果application.yml中指定了相同的配置項(xiàng),則它會(huì)覆蓋extra.yml中對(duì)應(yīng)的配置項(xiàng):
# application.yml,會(huì)覆蓋extra.yml中的相同配置項(xiàng) extra: name: 李四 order: 4
當(dāng)然,如果使用了環(huán)境配置文件application-{profile}.yml,則它的生效優(yōu)先級(jí)又會(huì)高于application.yml。
另外,@PropertySource支持引入多個(gè)配置文件。例如,在引入extra.yml的同時(shí),引入對(duì)應(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", // 配置文件加載工廠(chǎng) factory = YamlPropertySourceFactory.class) public class ExtraConfig { }
這里,Spring會(huì)將占位符${spring.profiles.active}解析為對(duì)應(yīng)的值。例如,在application.yml中指定spring.profiles.active=dev,那么配置文件extra-dev.yml會(huì)被引入(如有),它的生效優(yōu)先級(jí)高于extra.yml,但低于application.yml。
# extra-dev.yml,會(huì)覆蓋extra.yml中的相同配置項(xiàng) extra: name: 王五 order: 5
總結(jié)
- @PropertySource用于引入額外的配置文件。
- 通過(guò)自定義配置文件加載工廠(chǎng),可支持YAML文件解析,并支持ignoreResourceNotFound。
- 配置文件生效的優(yōu)先級(jí)順序?yàn)椋篴pplication-{profile}.yml>application.yml>extra-{profile}.yml>extra.yml。
以上就是SpringBoot引入額外的YAML配置文件的代碼實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot引入額外YAML文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java數(shù)據(jù)類(lèi)型與變量的安全性介紹
這篇文章主要介紹了java數(shù)據(jù)類(lèi)型與變量的安全性介紹,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法
在實(shí)際項(xiàng)目使用中,必須要考慮服務(wù)的安全性,當(dāng)服務(wù)部署到互聯(lián)網(wǎng)以后,就要考慮服務(wù)被惡意請(qǐng)求和暴力攻擊的情況,所以本文給大家介紹了SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法,需要的朋友可以參考下2024-11-11淺談Java中隨機(jī)數(shù)的幾種實(shí)現(xiàn)方式
這篇文章主要介紹了Java中隨機(jī)數(shù)的幾種實(shí)現(xiàn)方式,從最簡(jiǎn)單的Math.random到多線(xiàn)程的并發(fā)實(shí)現(xiàn)都在本文所列之中,需要的朋友可以參考下2015-07-07spring boot請(qǐng)求異常處理并返回對(duì)應(yīng)的html頁(yè)面
這篇文章主要介紹了spring boot處理請(qǐng)求異常并返回對(duì)應(yīng)的html頁(yè)面,包括404異常處理和500異常處理,需要的朋友可以參考下2017-07-07Java集合類(lèi)的組織結(jié)構(gòu)和繼承、實(shí)現(xiàn)關(guān)系詳解
這篇文章主要介紹了Java集合類(lèi)的組織結(jié)構(gòu)和繼承、實(shí)現(xiàn)關(guān)系,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Spring Boot定時(shí)器創(chuàng)建及使用解析
這篇文章主要介紹了Spring Boot定時(shí)器創(chuàng)建及使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07