yml格式的配置文件感覺(jué)很人性化,所以想把項(xiàng)目中的.properties都替換成.yml文件,主要springboot自1.5以后就把@configurationProperties中的location參數(shù)去掉,各種查詢(xún)之后發(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)化、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(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)
猜你還喜歡下面的內(nèi)容