最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Boot 自定義PropertySourceLoader

 更新時(shí)間:2017年05月05日 17:16:22   作者:catoop  
這篇文章主要介紹了詳解Spring Boot 自定義PropertySourceLoader,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

SpringBoot 的配置文件內(nèi)置支持 properties、xml、yml、yaml 幾種格式,其中 properties和xml 對(duì)應(yīng)的Loader類(lèi)為 PropertiesPropertySourceLoader ,yml和yaml 對(duì)應(yīng)的Loader類(lèi)為 YamlPropertySourceLoader。

觀察這2個(gè)類(lèi)可以發(fā)現(xiàn),都實(shí)現(xiàn)自接口 PropertySourceLoader 。所以我們要新增支持別的格式的配置文件,就可以通過(guò)實(shí)現(xiàn)接口 PropertySourceLoader 來(lái)實(shí)現(xiàn)了。

下面實(shí)現(xiàn)了一個(gè) json 格式的配置文件 Loader類(lèi):

package com.shanhy.sboot.property;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;

/**
 * JSON格式配置文件加載器
 * 
 * @author 單紅宇(CSDN CATOOP)
 * @create 2017年4月20日
 */
public class JsonPropertySourceLoader implements PropertySourceLoader {

  public String[] getFileExtensions() {
    // 配置文件格式(擴(kuò)展名)
    return new String[] { "json" };
  }

  public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
    // 處理機(jī)制參考PropertiesPropertySourceLoader
    // 無(wú)論profile有沒(méi)有值,底層都會(huì)嘗試先執(zhí)行 load(String name, Resource resource, null),所以這個(gè)地方之間判斷等于null即可。
    // 當(dāng)前版本springboot-1.5.2(后續(xù)版本未知)詳見(jiàn) ConfigFileApplicationListener 的 445 行
    if (profile == null) {
      Map<String, Object> result = mapPropertySource(resource);
      return new MapPropertySource(name, result);
    }
    return null;
  }

  /**
   * 解析Resource為Map
   *
   * @param resource
   * @return
   * @throws IOException
   * 
   * @author 單紅宇(CSDN CATOOP)
   * @create 2017年4月20日
   */
  private Map<String, Object> mapPropertySource(Resource resource) throws IOException {
    if (resource == null) {
      return null;
    }
    Map<String, Object> result = new HashMap<String, Object>();
    JsonParser parser = JsonParserFactory.getJsonParser();
    Map<String, Object> map = parser.parseMap(readFile(resource));
    nestMap("", result, map);
    return result;
  }

  /**
   * 讀取Resource文件內(nèi)容為字符串
   *
   * @param resource
   * @return
   * @throws IOException
   * 
   * @author 單紅宇(CSDN CATOOP)
   * @create 2017年4月20日
   */
  private String readFile(Resource resource) throws IOException {
    InputStream inputStream = resource.getInputStream();
    List<Byte> byteList = new LinkedList<Byte>();
    byte[] readByte = new byte[1024];
    int length;
    while ((length = inputStream.read(readByte)) > 0) {
      for (int i = 0; i < length; i++) {
        byteList.add(readByte[i]);
      }
    }
    byte[] allBytes = new byte[byteList.size()];
    int index = 0;
    for (Byte soloByte : byteList) {
      allBytes[index] = soloByte;
      index += 1;
    }
    return new String(allBytes, "UTF-8");
  }

  /**
   * 處理map(map中可能還嵌套map,遞歸處理),最終輸出一個(gè)非嵌套的map
   *
   * @param prefix
   *      前綴
   * @param result
   *      處理后的map
   * @param map
   *      處理前的map
   * 
   * @author 單紅宇(CSDN CATOOP)
   * @create 2017年4月20日
   */
  @SuppressWarnings("unchecked")
  private void nestMap(String prefix, Map<String, Object> result, Map<String, Object> map) {
    if (prefix.length() > 0) {
      prefix += ".";
    }
    for (Map.Entry<String, Object> entrySet : map.entrySet()) {
      if (entrySet.getValue() instanceof Map) {
        nestMap(prefix + entrySet.getKey(), result, (Map<String, Object>) entrySet.getValue());
      } else {
        result.put(prefix + entrySet.getKey().toString(), entrySet.getValue());
      }
    }
  }
}

然后在 src/main/resources 中創(chuàng)建 META-INF/spring.factories 文件,內(nèi)容為:

org.springframework.boot.env.PropertySourceLoader=\
com.shanhy.sboot.property.JsonPropertySourceLoader

創(chuàng)建測(cè)試的配置文件 application.json

{
  "custom": {
    "property": {
      "message": "測(cè)試數(shù)據(jù)"
    }
  }
}

創(chuàng)建驗(yàn)證結(jié)果的 HelloController.Java

@RestController
public class HelloController {

  @Value("${custom.property.message:}")
  private String customProperty;

  @RequestMapping("/test")
  public String test() {
    return customProperty;
  }
}

啟動(dòng)工程服務(wù),瀏覽器訪問(wèn) http://localhost:8080/test 即可查看輸出的結(jié)果為 “測(cè)試數(shù)據(jù)”;

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

应用必备| 仙桃市| 神木县| 台北县| 班戈县| 五莲县| 屯门区| 泰州市| 威海市| 蚌埠市| 青岛市| 横峰县| 扶沟县| 禄劝| 宁海县| 庆元县| 萝北县| 开封县| 缙云县| 吉林市| 禄劝| 会泽县| 思南县| 兴山县| 茂名市| 沈丘县| 瓦房店市| 龙胜| 永和县| 平武县| 武陟县| 黔西| 从化市| 洪泽县| 贺兰县| 开江县| 靖宇县| 营山县| 平山县| 竹溪县| 泰来县|