- 如何使用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;
}
}

本文介绍了如何在Java中使用SnakeYAML库读取YML配置文件并将其转换为Map对象,详细阐述了配置文件的存储方式,并提供了一段测试代码展示解析过程。
9880

被折叠的 条评论
为什么被折叠?



