private String getPropertiesValue(String key) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("application.yml");
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(inputStream);
String[] split = key.split("\\.");
String res = null;
for (String s : split) {
Object ob = map.get(s);
if (!(ob instanceof Map)) {
res = (String) ob;
} else {
map = (Map) ob;
}
}
return res;
}
调用:
String key = "spring.profiles.active";
System.out.println(getPropertiesValue(key));