/**
* 读取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
相关推荐
安逸sgr2 分钟前
Hermes Agent + Obsidian 打造第二大脑(六):分层记忆系统的设计逻辑——L0/L1/L2/L3 四层记忆详解苍煜27 分钟前
一篇讲懂分库分表:概念、spirngboot实战梦想画家36 分钟前
PostgreSQL 物化视图实战:从数据固化到智能刷新的全链路指南weoptions40 分钟前
简单sql注入中如何通过简单语句判断注入类型&注入方法小短腿的代码世界1 小时前
Qt数据库编程深度解析:从SQL基础到ORM架构设计Database_Cool_1 小时前
在 RDS PostgreSQL 中实现 RaBitQ 量化【心态好不摆烂】1 小时前
MySQL操作库Javatutouhouduan1 小时前
Java小白如何快速玩转Redis?Lyyaoo.3 小时前
Redisson网络工程小王3 小时前
【LCEL 链式调用详解】调用篇-2