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

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

}
相关推荐
Hello.Reader1 分钟前
PyFlink DataStream 程序骨架、常用 Source/Sink、状态(State)、与 Table/SQL 互转一篇搞定
数据库·sql·linq
三不原则7 分钟前
故障案例:模型推理响应慢,排查 Redis 缓存集群问题
数据库·redis·缓存
alonewolf_9913 分钟前
MySQL Explain详解与索引优化实战
数据库·mysql·adb
それども18 分钟前
MySQL 查询索引最左前缀原则,如果是(a,b)的联合索引,WHERE b = ? AND a = ?会走索引吗
数据库·mysql
それども18 分钟前
MySQL EXPLAIN Impossible WHERE noticed after reading const tables
数据库·mysql
a程序小傲22 分钟前
得物Java面试被问:边缘计算的数据同步和计算卸载
java·开发语言·数据库·后端·面试·golang·边缘计算
Cx330❀23 分钟前
脉脉2026实测:【AI创作者xAMA】平台核心功能解析
数据库·人工智能·脉脉
星云数灵25 分钟前
大模型高级工程师考试练习题7
数据库·大模型·阿里云acp·大模型工程师·大模型考试题库·阿里云aca·大模型工程师acp
brave_zhao2 小时前
达梦8最终锁阻塞巡检 SQL
数据库
一 乐9 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端