/**
* 读取bootstrap配置文件
*/
@Component
public class YmlUtils {
private static final Map<String, String> confMap = new HashMap(256);
static {
ClassLoader classLoader = YmlUtils.class.getClassLoader();
Yaml yaml = new Yaml();
InputStream bootstrapIn = classLoader.getResourceAsStream("bootstrap.yml");
if (bootstrapIn != null) {
fillConfMap(yaml.loadAs(bootstrapIn, Map.class), confMap, null);
IOUtils.closeQuietly(bootstrapIn);
}
}
private static void fillConfMap(Map<String, Object> sourceMap, Map<String, String> destinationMap, String key) {
sourceMap.forEach((key1, v) -> {
String k = key != null ? key + "." + key1 : key1;
if (v instanceof Map) {
fillConfMap((Map) v, destinationMap, k);
} else {
destinationMap.put(k, String.valueOf(v));
}
});
}
public static String getConfig(String key, String defualtValue) {
String value = confMap.get(key);
return value != null && !"".equals(value.trim()) ? value : defualtValue;
}
public static String getConfig(String key) {
return confMap.get(key);
}
public static String getAppName(){
return getConfig("spring.application.name");
}
}
静态方法获取 配置文件的内容
guoyiguang22023-11-11 9:15
相关推荐
weixin_421133418 分钟前
django xadmin 结合 minio白云偷星子29 分钟前
MySQL笔记14绵绵细雨中的乡音1 小时前
MySQL 常用函数实操指南:从基础到实战案例wudl55662 小时前
Flink SQL 与 Kafka 整合详细教程凉栀お_2 小时前
MySQL相关知识查询表中内容(第二次作业)ss2733 小时前
手写Spring第7弹:Spring IoC容器深度解析:XML配置的完整指南PFinal社区_南丞3 小时前
PostgreSQL-10个鲜为人知的强大功能misty youth3 小时前
配置openguass 教程(自存)瑞士卷@4 小时前
MyBatis入门到精通(Mybatis学习笔记)白云偷星子4 小时前
MySQL笔记13