yml格式的配置文件感覺很人性化,所以想把項目中的.properties都替換成.yml文件,主要springboot自1.5以后就把@configurationProperties中的location參數(shù)去掉,各種查詢之后發(fā)現(xiàn)可以用YamlPropertySourceLoader去裝載yml文件,上代碼

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ResourceLoader loader = new DefaultResourceLoader();
YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader();
List<String> yamlFilePaths = new ArrayList<>();
while(true){
String yamlFilePath = environment.getProperty("load.yaml["+i+"]");
if(yamlFilePath==null){
break;
}
i++;
if("".equals(yamlFilePath)){
continue;
}
yamlFilePaths.add(yamlFilePath);
}
yamlFilePaths.forEach(filePath->{
try {
environment.getPropertySources().addLast(yamlLoader.load(filePath,loader.getResource(filePath),null));
} catch (IOException e) {
logger.error("load property file failed!file:" + filePath);
throw new RuntimeException(e);
}
});
}
當(dāng)前題目:springboot裝載自定義yml文件-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article42/ejshc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站策劃、建站公司、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容