/**
* 读取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
相关推荐
张洛闻Eren2 小时前
MySQL 管理复制拓扑【MySQL第四课】西安景驰电子2 小时前
《PTP精确时间协议系列》第二篇:工程部署、调试与性能优化xrkhy2 小时前
redis-shake的使用windows版本小王C语言3 小时前
MySQL 内置函数:日期函数、字符串函数、数学函数、其他函数ClouGence4 小时前
当 AI 开始直接操作数据库,传统数据库管理工具还有必要吗?teak_on_my_way4 小时前
外网通过 Nginx 访问 Doris Stream Load:一次 HTTP 307 疑难排查与完整代理方案Hugh-Yu-1301235 小时前
zhangxuefeng-skill配置教程布莱克6055 小时前
理解B+树:原理、特性与应用场景冰暮流星5 小时前
mysql之排序查询7177775 小时前
让权限治理成为可复制资产:Gitee Team 空间配置方案与安全级别落地详解