/**
* 读取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 小时前
Redis核心数据结构-Set数研小生1 小时前
亚马逊商品列表API详解洛豳枭薰1 小时前
MySQL 并行复制无尽的沉默1 小时前
Redis下载安装czlczl200209251 小时前
增删改查时如何提高Mysql与Redis的一致性打工的小王1 小时前
MySql(二)索引数据知道1 小时前
PostgreSQL 性能优化:如何提高数据库的并发能力?wengqidaifeng1 小时前
数据结构(三)栈和队列(上)栈:计算机世界的“叠叠乐”数据知道1 小时前
PostgreSQL性能优化:内存配置优化(shared_buffers与work_mem的黄金比例)静听山水1 小时前
Redis核心数据结构