静态方法获取 配置文件的内容

复制代码
/**
 * 读取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");
    }

}
相关推荐
2501_945423544 分钟前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
樹JUMP29 分钟前
Python虚拟环境(venv)完全指南:隔离项目依赖
jvm·数据库·python
用什么都重名32 分钟前
Redis 入门与实践:从基础到 Stream 消息队列
数据库·redis·缓存
Mistra丶36 分钟前
记一次 JVM+Postgresql的 “死锁” 问题排查
jvm·数据库·postgresql·死锁
一然明月37 分钟前
Qt QML 锚定(Anchors)全解析
java·数据库·qt
分享牛1 小时前
Operaton入门到精通23-Operaton 2.0 原生支持 JUnit 6 核心指南
数据库·junit
编码忘我1 小时前
mysq系列之事务
数据库
知识分享小能手1 小时前
Redis入门学习教程,从入门到精通,Redis进阶编程知识点详解(5)
数据库·redis·学习
MekoLi291 小时前
MongoDB 新手完全指南:从入门到精通的实战手册
数据库·后端
cyforkk1 小时前
Spring AOP 进阶:揭秘 @annotation 参数绑定的底层逻辑
java·数据库·spring