這篇文章主要講解了“springboot項(xiàng)目自定義讀取多環(huán)境yml配置方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“springboot項(xiàng)目自定義讀取多環(huán)境yml配置方法”吧!
目前創(chuàng)新互聯(lián)建站已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、宜秀網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
有一些支付相關(guān)的配置文件是在不想寫(xiě)到application里的。
不說(shuō)原理了,網(wǎng)上一大堆。
1:直接上代碼:
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.PropertySourceLoader; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.util.CollectionUtils; import org.springframework.util.ResourceUtils; import java.io.IOException; import java.util.List; /** * @version V1.0 * @description: 讀取自定義的payment-*.yml的環(huán)境處理器(只讀yml文件,其他文件忽略) * @author: SunF * @create: 2019-08-21 09:59 **/ @Slf4j public class PaymentEnvironmentPostProcessor implements EnvironmentPostProcessor { private final ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(); private final PropertySourceLoader propertySourceLoader; public PaymentEnvironmentPostProcessor() { super(); propertySourceLoader = new YamlPropertySourceLoader(); } @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { String[] activeProfiles = environment.getActiveProfiles(); for (String activeProfile: activeProfiles){ String location = ResourceUtils.CLASSPATH_URL_PREFIX + "paymentConfig/payment-"+activeProfile+ ".yml"; try{ Resource[] resourceArray = this.patternResolver.getResources(location); for(Resource resource : resourceArray){ List<PropertySource<?>> propertySourceList = propertySourceLoader.load(resource.getFilename(), resource); if(! CollectionUtils.isEmpty(propertySourceList)){ propertySourceList.stream().forEach(environment.getPropertySources() :: addLast); } } }catch(IOException e){ log.error("加載支付配置文件失敗,因?yàn)閧}", e.getMessage(), e); } } } }
這里只讀了yml文件,我們不需要其他文件,如果采用多種PropertySourceLoader進(jìn)行掃描加載無(wú)疑增加了不必要的啟動(dòng)時(shí)間。
如果有需要可以將propertySourceLoader改為L(zhǎng)ist,后續(xù)的環(huán)節(jié)在for循環(huán)中處理:
private final List<PropertySourceLoader> propertySourceLoaderList; public PaymentEnvironmentPostProcessor() { super(); this.propertySourceLoaderList = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader()); }
2:添加spring.factories
在resources下添加META-INF文件夾,在旗下創(chuàng)建spring.factories文件,內(nèi)容為:
org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.xxx.config.PaymentEnvironmentPostProcessor
感謝各位的閱讀,以上就是“springboot項(xiàng)目自定義讀取多環(huán)境yml配置方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)springboot項(xiàng)目自定義讀取多環(huán)境yml配置方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)頁(yè)題目:springboot項(xiàng)目自定義讀取多環(huán)境yml配置方法
文章轉(zhuǎn)載:http://bm7419.com/article30/pschpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、微信公眾號(hào)、虛擬主機(jī)、自適應(yīng)網(wǎng)站、網(wǎng)站排名、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)