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

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

}
相关推荐
张洛闻Eren2 小时前
MySQL 管理复制拓扑【MySQL第四课】
linux·运维·数据库·mysql
西安景驰电子2 小时前
《PTP精确时间协议系列》第二篇:工程部署、调试与性能优化
运维·服务器·网络·数据库·windows·性能优化
xrkhy2 小时前
redis-shake的使用windows版本
数据库·windows·redis
小王C语言3 小时前
MySQL 内置函数:日期函数、字符串函数、数学函数、其他函数
数据库·mysql
ClouGence4 小时前
当 AI 开始直接操作数据库,传统数据库管理工具还有必要吗?
数据库·后端·agent
teak_on_my_way4 小时前
外网通过 Nginx 访问 Doris Stream Load:一次 HTTP 307 疑难排查与完整代理方案
运维·数据库·nginx·http
Hugh-Yu-1301235 小时前
zhangxuefeng-skill配置教程
数据库
布莱克6055 小时前
理解B+树:原理、特性与应用场景
数据结构·数据库·mysql
冰暮流星5 小时前
mysql之排序查询
数据库·mysql
7177775 小时前
让权限治理成为可复制资产:Gitee Team 空间配置方案与安全级别落地详解
数据库·gitee