/**
* 读取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
相关推荐
悟能不能悟11 分钟前
mysql的not exists走索引吗明月与玄武12 分钟前
Jmeter -- JDBC驱动连接数据库超详细指南专注VB编程开发20年13 分钟前
VB.NET关于接口实现与简化设计的分析,封装其他类vvilkim18 分钟前
Redis持久化机制详解:保障数据安全的关键策略cooldream200925 分钟前
信息安全的基石:深入理解五大核心安全服务大数据魔法师36 分钟前
Redis(三) - 使用Java操作Redis详解noravinsc39 分钟前
e.g. ‘django.db.models.BigAutoField‘.IT光1 小时前
Redis 五种类型基础操作(redis-cli + Spring Data Redis)TiDB 社区干货传送门2 小时前
从40秒到11毫秒:TiDB环境下一次SQL深潜优化实战IP管家2 小时前
企业级IP代理解决方案:负载均衡与API接口集成实践