获取自定义yml内容代码
package;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class Main {
private static final Map<String, String> envVariable = new HashMap<String, String>();
public static void main(String[] args) {
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("ws.yml");
Yaml yaml = new Yaml();
Map map = yaml.loadAs(inputStream, Map.class);
parseMap(map, null);
String ws = envVariable.get("ws");
System.out.println("ws = " + ws);
}
private static void parseMap(Map map, String pre) {
map.forEach((k, v) -> {
if (v instanceof Map) {
parseMap((Map) v, pre == null ? (String) k : pre + "." + k);
} else {
envVariable.put(pre == null ? (String) k : pre + "." + k, String.valueOf(v));
}
});
}
}
ws.yml
文件
# ws配置
ws:
- puburl: xxxx
pubclass: xxxxxx
- puburl: xxxx
pubclass: xxxxxx
输出结果:
ws = [{puburl=xxxx, pubclass=xxxxxx}, {puburl=xxxx, pubclass=xxxxxx}]