/**
* 读取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
相关推荐
等....3 小时前
Minio使用win x4 小时前
Redis 使用~如何在Java中连接使用redis迷枫7125 小时前
DM8 数据库安装实战:从零搭建达梦数据库环境(附全套工具链接)XDHCOM6 小时前
PostgreSQL 25001: active_sql_transaction 报错原因分析,故障修复步骤详解,远程处理解决方案卤炖阑尾炎6 小时前
PostgreSQL 日常运维全指南:从基础操作到备份恢复daad7777 小时前
wifi_notexixingzhe27 小时前
Mysql统计空间增量程序员萌萌8 小时前
Redis的缓存机制和淘汰策略详解不剪发的Tony老师8 小时前
SQLite 3.53.0版本发布,重要更新Bczheng19 小时前
九.Berkeley DB数据库 序列化和钱包管理(1)