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

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

}
相关推荐
weixin_421133418 分钟前
django xadmin 结合 minio
数据库·django·sqlite
白云偷星子29 分钟前
MySQL笔记14
数据库·笔记·mysql
绵绵细雨中的乡音1 小时前
MySQL 常用函数实操指南:从基础到实战案例
数据库·mysql
wudl55662 小时前
Flink SQL 与 Kafka 整合详细教程
sql·flink·kafka
凉栀お_2 小时前
MySQL相关知识查询表中内容(第二次作业)
数据库·mysql
ss2733 小时前
手写Spring第7弹:Spring IoC容器深度解析:XML配置的完整指南
java·前端·数据库
PFinal社区_南丞3 小时前
PostgreSQL-10个鲜为人知的强大功能
数据库·后端
misty youth3 小时前
配置openguass 教程(自存)
数据库·ubuntu·华为·openguass
瑞士卷@4 小时前
MyBatis入门到精通(Mybatis学习笔记)
java·数据库·后端·mybatis
白云偷星子4 小时前
MySQL笔记13
数据库·笔记·mysql