springboot裝載自定義yml文件-創(chuàng)新互聯(lián)

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

十多年專注成都網(wǎng)站制作,成都定制網(wǎng)站,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)站,高端網(wǎng)頁制作,對自上料攪拌車等多個行業(yè),擁有豐富的營銷推廣經(jīng)驗。
 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);
      }

    });


  }

當前題目:springboot裝載自定義yml文件-創(chuàng)新互聯(lián)
文章起源:http://muchs.cn/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)

微信小程序開發(fā)