Yaml 配置檔案解析Map-SnakeYAML
阿新 • • 發佈:2021-02-11
技術標籤:java學習筆記ymljavaSnakeYamlyml
- 如何使用SnakeYAML來讀取YAML(YML為其簡寫)配置檔案;
- 讀取後,裝載成Map,而Map如何儲存的配置檔案的資料。
Snake POM引入
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.27</version>
</dependency>
讀取YAML配置檔案
InputStream in = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_RESOURCES); Yaml yaml = new Yaml(); Map<String, Object> load = yaml.loadAs(in, Map.class);
Map是如何儲存的配置檔案資料
以下面的.yml配置檔案為例:
log:
location: /home/iwu/runningstat
cache:
path: /home/iwu/runningstat/temp
local: false
appid: 123
server:
port: 2020
app:
testList:
-
name: wangchao
age: 23
-
name: sff
age: 20
使用SnakeYAML進行裝載,例項化成Map物件(確切的說,應該是Map的子類,LinkedHashMap)。值得注意的是,要裝載如此複雜的配置檔案的,必須使用Map的巢狀,即:Map巢狀Map。
先來一段測試程式碼:
private void printYmalMap(String prefixKey, Map<String, Object> map) { String fullKey = ""; for (String key : map.keySet()) { fullKey = prefixKey == null || "".equals(prefixKey) ? key : prefixKey + "." + key; Object value = map.get(key); if (value instanceof Map) { //System.out.println("there is a map " + prefixKey + "." + key); printYmalMap(fullKey, (Map) value); } else if (value instanceof List) { System.out.println("there is a list " + fullKey); } else { System.out.println("there is a "+value.getClass().getName() +"... "+ fullKey); } } }
看看Snake解析上面yml檔案列印結果
分析完成,看看完整的yaml載入 程式碼。這裡我們將基本型別轉換為了String型別。如有需要我們可以進一步進行處理。
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.*;
public class YamlResourcesProcessor{
private static final String DEFAULT_RESOURCES = "application.yml";
/**
* ymal 檔案中資料解析快取
*/
private static volatile Map<String, String> cache = Collections.synchronizedMap(new HashMap<>());
public YamlResourcesProcessor() {
load();
}
private void load() {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_RESOURCES);
Yaml yaml = new Yaml();
Map<String, Object> load = yaml.loadAs(in, Map.class);
printYmalMap(null, load);
cacheYmal(null, load);
}
private void cacheYmal(String prefixKey, Map<String, Object> map) {
String fullKey = "";
for (String key : map.keySet()) {
Object value = map.get(key);
fullKey = prefixKey == null || "".equals(prefixKey) ? key : prefixKey + "." + key;
if (value instanceof Map) {
cacheYmal(fullKey, (Map) value);
} else if (value instanceof List) {
// Ymal轉Map 暫時不處理List
cache.put(fullKey, String.valueOf(value));
//continue;
} else {
cache.put(fullKey, String.valueOf(value));
}
}
}
private void printYmalMap(String prefixKey, Map<String, Object> map) {
String fullKey = "";
for (String key : map.keySet()) {
fullKey = prefixKey == null || "".equals(prefixKey) ? key : prefixKey + "." + key;
Object value = map.get(key);
if (value instanceof Map) {
//System.out.println("there is a map " + prefixKey + "." + key);
printYmalMap(fullKey, (Map) value);
} else if (value instanceof List) {
System.out.println("there is a list " + fullKey);
} else {
System.out.println("there is a "+value.getClass().getName() +"... "+ fullKey);
}
}
}
public Map<String, String> getResources() {
return cache;
}
}