/**
* 读取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
相关推荐
程序员卷卷狗1 小时前
MySQL 高可用方案:主从 + MHA + ProxySQL + PXC 的实战应用与架构思考千千寰宇2 小时前
[数据库/数据结构] LSM-Tree :结构化的日志合并树——NewSQL数据库的基石韩立学长3 小时前
基于Springboot的研学旅游服务系统5u416w14(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。isNotNullX3 小时前
怎么理解ETL增量抽取?谅望者3 小时前
数据分析笔记14:Python文件操作l1t3 小时前
调用python函数的不同方法效率对比测试武昌库里写JAVA3 小时前
微擎服务器配置要求,微擎云主机多少钱一年?honortech3 小时前
MySQL 8 连接报错:Public Key Retrieval is not allowedq***82913 小时前
MySQL--》如何通过选择合适的存储引擎提高查询效率?q***96583 小时前
MySql-9.1.0安装详细教程(保姆级)