/**
* 读取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
相关推荐
可涵不会debug4 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围ByteBlossom4 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?麦兜*4 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群Slaughter信仰4 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)麦兜*4 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案-Xie-5 小时前
Mysql杂志(十六)——缓存池七夜zippoe5 小时前
缓存与数据库一致性实战手册:从故障修复到架构演进一个天蝎座 白勺 程序猿6 小时前
Apache IoTDB(5):深度解析时序数据库 IoTDB 在 AINode 模式单机和集群的部署与实践