yml格式的配置文件感覺(jué)很人性化,所以想把項(xiàng)目中的.properties都替換成.yml文件,主要springboot自1.5以后就把@configurationProperties中的location參數(shù)去掉,各種查詢之后發(fā)現(xiàn)可以用YamlPropertySourceLoader去裝載yml文件,上代碼
十多年專注成都網(wǎng)站制作,成都定制網(wǎng)站,個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)站,高端網(wǎng)頁(yè)制作,對(duì)自上料攪拌車(chē)等多個(gè)行業(yè),擁有豐富的營(yíng)銷(xiāo)推廣經(jīng)驗(yàn)。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); } }); }