您好,登錄后才能下訂單哦!
這篇文章主要講解了“springboot項目自定義讀取多環(huán)境yml配置方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“springboot項目自定義讀取多環(huán)境yml配置方法”吧!
有一些支付相關(guān)的配置文件是在不想寫到application里的。
不說原理了,網(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("加載支付配置文件失敗,因為{}", e.getMessage(), e); } } } }
這里只讀了yml文件,我們不需要其他文件,如果采用多種PropertySourceLoader進(jìn)行掃描加載無疑增加了不必要的啟動時間。
如果有需要可以將propertySourceLoader改為List,后續(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項目自定義讀取多環(huán)境yml配置方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對springboot項目自定義讀取多環(huán)境yml配置方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。