SpringBoot如何啟動(dòng)自動(dòng)加載自定義模塊yml文件(PropertySourceFactory)
參考:https://deinum.biz/2018-07-04-PropertySource-with-yaml-files/
背景
自定義模塊時(shí)經(jīng)常會(huì)編寫當(dāng)前模塊的一些核心yml配置,如果要被Spring容器加載到通常的做法是將自定義模塊的yml命名為application-模塊名.yml,比如db模塊命名為application-db.yml,然后再在spring.profiles.include中引入該db,即可加載到子模塊配置(前提是maven構(gòu)建時(shí)能夠?qū)esources資源目錄編譯進(jìn)去)。
上面說(shuō)的這種限制有很多,子模塊對(duì)于父模塊是黑箱操作,我們無(wú)法得知有多少個(gè)子模塊要被include進(jìn)來(lái),而且前綴必須以application開頭。
今天我們來(lái)看另外一種更加靈活的方式,自定義名稱的yml直接在子模塊中加載,各位朋友繼續(xù)往下看PropertySourceFactory如何實(shí)現(xiàn)。
總結(jié)3步走
定義一個(gè)YamlPropertySourceFactory,實(shí)現(xiàn)PropertySourceFactory,重寫createPropertySource方法
將接口給過(guò)來(lái)的資源流文件使用加載到spring提供的YmlPropertiesFactorBean中,由該工廠Bean產(chǎn)出一個(gè)Properties對(duì)象
將生成的Properties對(duì)象傳入PropertiesPropertySource中產(chǎn)出一個(gè)PropertySource對(duì)象
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* @description:
* @author: jianjun.ren
* @date: Created in 2020/9/24 12:05
*/
@AllArgsConstructor
public class YamlPropertySourceFactory implements PropertySourceFactory {
//這個(gè)方法有2個(gè)入?yún)?,分別為資源名稱和資源對(duì)象,這是第一步
public PropertySource< ? > createPropertySource(String name, EncodedResource resource) throws IOException {
//這是第二步,根據(jù)流對(duì)象產(chǎn)出Properties對(duì)象
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
assert sourceName != null;
//這是第三部,根據(jù)Properties對(duì)象,產(chǎn)出PropertySource對(duì)象,放入到Spring中
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) {
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException) {
throw (FileNotFoundException) e.getCause();
}
throw e;
}
}
}
創(chuàng)建咱們是說(shuō)完了,小伙伴有很多問(wèn)號(hào),怎么去使用呢?
接下來(lái)我們?cè)倏纯悼担?/strong>
在任意一個(gè)@Configuration類中,你自己寫一個(gè)也可以,反正能夠被Spring所掃描的
加入下面這段代碼,其實(shí)就一行:
@Configuration
//這個(gè)地方的YamlPropertySourceFactory就是上面編寫的代碼類
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:xxx-xxx.yml")
public class XXXXConfiguration {
}
最后
好了到這里就結(jié)束了,去使用看看效果吧~
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot通過(guò)Junit實(shí)現(xiàn)單元測(cè)試過(guò)程解析
這篇文章主要介紹了Spring Boot通過(guò)Junit實(shí)現(xiàn)單元測(cè)試過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Spring Boot配置讀取實(shí)現(xiàn)方法解析
這篇文章主要介紹了Spring Boot配置讀取實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
github上的java項(xiàng)目怎么運(yùn)行(面向小白)
這篇文章主要介紹了github上的java項(xiàng)目怎么運(yùn)行(面向小白),今天從github把我以前寫的一個(gè)小demo下載下來(lái)了,第一次下載項(xiàng)目,摸索了一個(gè)多小時(shí),才運(yùn)行起來(lái),需要的朋友可以參考下2019-06-06

