新聞中心
yml格式的配置文件感覺很人性化,所以想把項目中的.properties都替換成.yml文件,主要springboot自1.5以后就把@configurationProperties中的location參數(shù)去掉,各種查詢之后發(fā)現(xiàn)可以用YamlPropertySourceLoader去裝載yml文件,上代碼
站在用戶的角度思考問題,與客戶深入溝通,找到鄠邑網(wǎng)站設計與鄠邑網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站建設、成都網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、雅安服務器托管、企業(yè)郵箱。業(yè)務覆蓋鄠邑地區(qū)。
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ResourceLoader loader = new DefaultResourceLoader(); YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader(); ListyamlFilePaths = 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); } }); }
這里主要實現(xiàn)了spring boot的ApplicationListener
1.ApplicationStartedEvent spring boot 剛啟動時會觸發(fā)事件
2.ApplicationEnvironemntPreparedEvent spring boot 完成Environment的裝載但是還沒有開始applicationContext的裝載的時候觸發(fā)(它和實現(xiàn)了EnvironmentAware不一樣,后者時需要Bean被裝載進去后才調(diào)用)
3.ApplicationPreparedEvent springboot 完成上下文的創(chuàng)建,單還沒有完全完成bean的裝載
4.ApplicationFailedEvent spring boot啟動異常時觸發(fā)。
spring boot內(nèi)部本身就有很多l(xiāng)istener,他們分別監(jiān)聽上面幾種事件,這里就不再贅述,有興趣的同學可以研究一下spring boot的源碼。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文標題:springboot裝載自定義yml文件
網(wǎng)頁地址:http://www.dlmjj.cn/article/ghpogc.html