/**
* 读取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
相关推荐
小伍_Five1 小时前
使用Java操作Neo4j数据库码农不惑1 小时前
Django的定制以及adminElastic 中国社区官方博客2 小时前
Elasticsearch 向量数据库,原生支持 Google Cloud Vertex AI 平台一个数据大开发4 小时前
如何将excel数据快速导入数据库一介草民丶6 小时前
Mysql | 主从复制的工作机制酱学编程9 小时前
redis 延迟双删xujiangyan_11 小时前
MySQL的半同步模式飞翔沫沫情11 小时前
《MySQL 5.7.44审计合规实践:插件集成与日志分割自动化方案》MXsoft61811 小时前
云原生运维在 2025 年的发展蓝图不辉放弃12 小时前
SQL 主键(Primary Key)