欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring Boot 自定義PropertySourceLoader

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

SpringBoot 的配置文件內置支持 properties、xml、yml、yaml 幾種格式,其中 properties和xml 對應的Loader類為 PropertiesPropertySourceLoader ,yml和yaml 對應的Loader類為 YamlPropertySourceLoader。

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

下面實現(xiàn)了一個 json 格式的配置文件 Loader類:

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() {
    // 配置文件格式(擴展名)
    return new String[] { "json" };
  }

  public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
    // 處理機制參考PropertiesPropertySourceLoader
    // 無論profile有沒有值,底層都會嘗試先執(zhí)行 load(String name, Resource resource, null),所以這個地方之間判斷等于null即可。
    // 當前版本springboot-1.5.2(后續(xù)版本未知)詳見 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文件內容為字符串
   *
   * @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,遞歸處理),最終輸出一個非嵌套的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 文件,內容為:

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

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

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

創(chuàng)建驗證結果的 HelloController.Java

@RestController
public class HelloController {

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

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot進行Web開發(fā)的實現(xiàn)

    SpringBoot進行Web開發(fā)的實現(xiàn)

    Spring?Boot讓我們可以快速構建項目并運行web應用,大大簡化了Spring的復雜配置,本文主要介紹了SpringBoot進行Web開發(fā)的實現(xiàn),感興趣的可以了解一下
    2023-10-10
  • 基于java中集合的概念(詳解)

    基于java中集合的概念(詳解)

    下面小編就為大家?guī)硪黄趈ava中集合的概念(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • SpringBoot實現(xiàn)對Http接口進行監(jiān)控的代碼

    SpringBoot實現(xiàn)對Http接口進行監(jiān)控的代碼

    Spring Boot Actuator是Spring Boot提供的一個模塊,用于監(jiān)控和管理Spring Boot應用程序的運行時信息,本文將介紹一下Spring Boot Actuator以及代碼示例,以及如何進行接口請求監(jiān)控,需要的朋友可以參考下
    2024-07-07
  • Springboot項目Aop與攔截器與過濾器橫向對比

    Springboot項目Aop與攔截器與過濾器橫向對比

    前三篇文章已經介紹過Springboot項目如何實現(xiàn)Aop,攔截器和過濾齊功能,這篇文章主要介紹三者的橫向對比,本文有一定的參考價值,感興趣的小伙伴可以參考閱讀
    2023-03-03
  • Spring配置文件使用占位符配置方式

    Spring配置文件使用占位符配置方式

    這篇文章主要介紹了Spring配置文件使用占位符配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Guava?Retryer實現(xiàn)接口重試的示例

    Guava?Retryer實現(xiàn)接口重試的示例

    本文主要介紹了Guava?Retryer實現(xiàn)接口重試的示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • SpringBoot實現(xiàn)接口冪等性的4種方案

    SpringBoot實現(xiàn)接口冪等性的4種方案

    這篇文章主要介紹了SpringBoot實現(xiàn)接口冪等性的4種方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別

    Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別

    這篇文章主要給大家介紹了關于Spring Boot處理全局統(tǒng)一異常的兩種方法與區(qū)別,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • 關于Controller層和Service層的類報錯問題及解決方案

    關于Controller層和Service層的類報錯問題及解決方案

    這篇文章主要介紹了關于Controller層和Service層的類報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Springboot整合https的實例代碼

    Springboot整合https的實例代碼

    本文簡單介紹了一些密碼學的基礎和如何通過Springboot整合HTTPS,本文將通過實例代碼給大家詳細介紹整合過程,感興趣的朋友跟隨小編一起看看吧
    2022-02-02

最新評論