springboot裝載自定義yml文件

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

站在用戶的角度思考問題,與客戶深入溝通,找到鄠邑網(wǎng)站設(shè)計(jì)與鄠邑網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋鄠邑地區(qū)。

 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);
      }

    });


  }

這里主要實(shí)現(xiàn)了spring boot的ApplicationListener<ApplicationEnvironmentPreparedEvent>接口,spring boot為我們提供了四種監(jiān)聽事件:

1.ApplicationStartedEvent   spring boot 剛啟動(dòng)時(shí)會(huì)觸發(fā)事件

2.ApplicationEnvironemntPreparedEvent    spring boot 完成Environment的裝載但是還沒有開始applicationContext的裝載的時(shí)候觸發(fā)(它和實(shí)現(xiàn)了EnvironmentAware不一樣,后者時(shí)需要Bean被裝載進(jìn)去后才調(diào)用)

3.ApplicationPreparedEvent   springboot 完成上下文的創(chuàng)建,單還沒有完全完成bean的裝載

4.ApplicationFailedEvent   spring boot啟動(dòng)異常時(shí)觸發(fā)。

spring boot內(nèi)部本身就有很多l(xiāng)istener,他們分別監(jiān)聽上面幾種事件,這里就不再贅述,有興趣的同學(xué)可以研究一下spring boot的源碼。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞名稱:springboot裝載自定義yml文件
文章URL:http://www.muchs.cn/article12/ghpogc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)自適應(yīng)網(wǎng)站、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)