/**
* 读取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
相关推荐
2501_945423544 分钟前
使用PyTorch构建你的第一个神经网络樹JUMP29 分钟前
Python虚拟环境(venv)完全指南:隔离项目依赖用什么都重名32 分钟前
Redis 入门与实践:从基础到 Stream 消息队列Mistra丶36 分钟前
记一次 JVM+Postgresql的 “死锁” 问题排查一然明月37 分钟前
Qt QML 锚定(Anchors)全解析分享牛1 小时前
Operaton入门到精通23-Operaton 2.0 原生支持 JUnit 6 核心指南编码忘我1 小时前
mysq系列之事务知识分享小能手1 小时前
Redis入门学习教程,从入门到精通,Redis进阶编程知识点详解(5)MekoLi291 小时前
MongoDB 新手完全指南:从入门到精通的实战手册cyforkk1 小时前
Spring AOP 进阶:揭秘 @annotation 参数绑定的底层逻辑