springboot如何通過@PropertySource加載自定義yml文件
@PropertySource加載自定義yml文件
使用@PropertySource默認加載的是.xml或者 .properties文件,因為在注解源碼默認使用的是DefaultPropertySourceFactory實現(xiàn)處理文件內(nèi)容,spring使用ResourcePropertySource從Resource構(gòu)建Properties傳給Spring。
系統(tǒng)的應(yīng)用,比如加載自定義的文件,將配置文件內(nèi)容存儲在內(nèi)存,如下:
那么加載一個自定義的.yml文件,就需要自定義實現(xiàn)ResourcePropertySource來處理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注解對于yml的支持
@PropertySource只對properties文件可以進行加載,但對于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 ;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java讀取文件:char的ASCII碼值=65279,顯示是一個空字符的解決
這篇文章主要介紹了java讀取文件:char的ASCII碼值=65279,顯示是一個空字符的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼
這篇文章主要介紹了application作用域?qū)崿F(xiàn)用戶登錄擠掉之前登錄用戶代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無法加載的問題
下面小編就為大家?guī)硪黄鉀QspringMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無法加載的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10java synchronized實現(xiàn)可見性過程解析
這篇文章主要介紹了java synchronized實現(xiàn)可見性過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09