springboot如何通過(guò)@PropertySource加載自定義yml文件
@PropertySource加載自定義yml文件
使用@PropertySource默認(rèn)加載的是.xml或者 .properties文件,因?yàn)樵谧⒔庠创a默認(rèn)使用的是DefaultPropertySourceFactory實(shí)現(xiàn)處理文件內(nèi)容,spring使用ResourcePropertySource從Resource構(gòu)建Properties傳給Spring。

系統(tǒng)的應(yīng)用,比如加載自定義的文件,將配置文件內(nèi)容存儲(chǔ)在內(nèi)存,如下:

那么加載一個(gè)自定義的.yml文件,就需要自定義實(shí)現(xiàn)ResourcePropertySource來(lái)處理yml文件的類


public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
@PropertySource注解對(duì)于yml的支持
@PropertySource只對(duì)properties文件可以進(jìn)行加載,但對(duì)于yml或者yaml不能支持。
追尋源碼。
public class DefaultPropertySourceFactory implements PropertySourceFactory {
? ? public DefaultPropertySourceFactory() {
? ? }
? ? public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
? ? ? ? return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
? ? }
}我們只需要繼承DefaultPropertySourceFactory類并修改就可以了。
public class YamlConfigFactory extends DefaultPropertySourceFactory {
? ? @Override
? ? public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
? ? ? ? String sourceName = name != null ? name : resource.getResource().getFilename();
? ? ? ? if (!resource.getResource().exists()) {
? ? ? ? ? ? return new PropertiesPropertySource(sourceName, new Properties());
? ? ? ? } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
? ? ? ? ? ? Properties propertiesFromYaml = loadYml(resource);
? ? ? ? ? ? return new PropertiesPropertySource(sourceName, propertiesFromYaml);
? ? ? ? } else {
? ? ? ? ? ? return super.createPropertySource(name, resource);
? ? ? ? }
? ? }
? ? private Properties loadYml(EncodedResource resource) throws IOException {
? ? ? ? YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
? ? ? ? factory.setResources(resource.getResource());
? ? ? ? factory.afterPropertiesSet();
? ? ? ? return factory.getObject();
? ? }
}@PropertySource(value = {"classpath:dog.yml"},factory = YamlConfigFactory.class)
@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
? ? private String name ;
? ? private String age ;以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用spring?boot+WebSocket實(shí)現(xiàn)后臺(tái)主動(dòng)消息推送功能
目前對(duì)于服務(wù)端向客戶端推送數(shù)據(jù),常用技術(shù)方案有輪詢、websocket等,下面這篇文章主要給大家介紹了關(guān)于利用spring?boot+WebSocket實(shí)現(xiàn)后臺(tái)主動(dòng)消息推送功能的相關(guān)資料,需要的朋友可以參考下2022-04-04
MyBatis 探秘之#{} 與 ${} 參傳差異解碼(數(shù)據(jù)庫(kù)連接池筑牢數(shù)據(jù)交互
本文詳細(xì)介紹了MyBatis中的`#{}`和`${}`的區(qū)別與使用場(chǎng)景,包括預(yù)編譯SQL和即時(shí)SQL的區(qū)別、安全性問(wèn)題,以及如何正確使用數(shù)據(jù)庫(kù)連接池來(lái)提高性能,感興趣的朋友一起看看吧2024-12-12
Spring Boot 集成Mybatis實(shí)現(xiàn)主從(多數(shù)據(jù)源)分離方案示例
本篇文章主要介紹了Spring Boot 集成Mybatis實(shí)現(xiàn)主從(多數(shù)據(jù)源)分離方案實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03
解決idea2020 maven無(wú)法自動(dòng)導(dǎo)包的問(wèn)題
這篇文章主要介紹了解決idea2020 maven無(wú)法自動(dòng)導(dǎo)包的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Java實(shí)現(xiàn)統(tǒng)計(jì)字符串出現(xiàn)的次數(shù)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)統(tǒng)計(jì)字符串出現(xiàn)的次數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
spring boot加載第三方j(luò)ar包的配置文件的方法
本篇文章主要介紹了spring boot加載第三方j(luò)ar包的配置文件的方法,詳細(xì)的介紹了spring boot jar包配置文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

