激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring Boot 自定義PropertySourceLoader

詳解Spring Boot 自定義PropertySourceLoader

2020-09-25 10:57catoop Java教程

這篇文章主要介紹了詳解Spring Boot 自定義PropertySourceLoader,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

下面實現了一個 json 格式的配置文件 Loader類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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有沒有值,底層都會嘗試先執行 load(String name, Resource resource, null),所以這個地方之間判斷等于null即可。
    // 當前版本springboot-1.5.2(后續版本未知)詳見 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 中創建 META-INF/spring.factories 文件,內容為:

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

創建測試的配置文件 application.json

?
1
2
3
4
5
6
7
{
  "custom": {
    "property": {
      "message": "測試數據"
    }
  }
}

創建驗證結果的 HelloController.Java

?
1
2
3
4
5
6
7
8
9
10
11
@RestController
public class HelloController {
 
  @Value("${custom.property.message:}")
  private String customProperty;
 
  @RequestMapping("/test")
  public String test() {
    return customProperty;
  }
}

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

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

原文鏈接:http://blog.csdn.net/catoop/article/details/71157986

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本在线播放一区二区 | 女人久久久www免费人成看片 | 亚洲最大的成人网 | 午夜精品久久久久久久久久久久久蜜桃 | 91,视频免费看 | 久久99久久99精品 | 毛片毛片免费看 | 中文字幕在线日韩 | 国产亚洲精品成人 | 免费放黄网站在线播放 | 日本在线一区二区 | 粉嫩蜜桃麻豆免费大片 | 国产成人在线网站 | 午夜视频在线免费 | 毛片视频大全 | 国产亚洲精品久久久久久久久久 | 国产乱淫av | 午夜色视频在线观看 | 国产成人在线播放视频 | 一本色道久久99精品综合蜜臀 | 国产精品伦视频看免费三 | 亚洲天堂字幕 | 精品国产一区二区三区天美传媒 | 美女久久久久久久久 | 成人小视频在线播放 | 免费看一级视频 | 高清av免费 | 国产成人自拍av | 国产精品毛片无码 | 国产乱xxxx| www.com超碰 | 操碰 | 免费久久久久久 | 国产精品午夜未成人免费观看 | 久久99精品久久久久久久久久久久 | 成人午夜视频免费 | 欧美在线成人影院 | 成人毛片视频在线播放 | aaaaaaa毛片 | 久久国产综合视频 | 久久福利小视频 |