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文件的類(lèi)
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類(lèi)并修改就可以了。
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)文章
java讀取文件:char的ASCII碼值=65279,顯示是一個(gè)空字符的解決
這篇文章主要介紹了java讀取文件:char的ASCII碼值=65279,顯示是一個(gè)空字符的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08application作用域?qū)崿F(xiàn)用戶(hù)登錄擠掉之前登錄用戶(hù)代碼
這篇文章主要介紹了application作用域?qū)崿F(xiàn)用戶(hù)登錄擠掉之前登錄用戶(hù)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Java實(shí)體類(lèi)中Set按照對(duì)象的某個(gè)字段對(duì)set排序
這篇文章主要介紹了Java實(shí)體類(lèi)中Set按照對(duì)象的某個(gè)字段對(duì)set排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10java synchronized實(shí)現(xiàn)可見(jiàn)性過(guò)程解析
這篇文章主要介紹了java synchronized實(shí)現(xiàn)可見(jiàn)性過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java連接redis報(bào)錯(cuò)timed?out問(wèn)題的解決辦法
最近項(xiàng)目開(kāi)發(fā)中用到了Redis,下面這篇文章主要給大家介紹了關(guān)于Java連接redis報(bào)錯(cuò)timed?out問(wèn)題的解決辦法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02