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

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

}
相关推荐
Menior_2 小时前
【MySQL】基本查询
数据库·mysql
一只游鱼3 小时前
Redis入门(部署、持久化、缓存问题)
数据库·redis·缓存
北城以北88884 小时前
数据库--MySQL数据管理
数据库·mysql
代码的余温4 小时前
Oracle RAC共享存储核心技术
数据库·oracle
float_六七4 小时前
数据库物理外键与逻辑外键全解析
数据库·oracle
大白的编程日记.4 小时前
【MySQL】数据库的基本操作
数据库·mysql·oracle
Jamie Chyi4 小时前
【Oracle经验分享】字符串拼接过长问题的解决方案 —— 巧用 XMLAGG
数据库·oracle
代码的余温4 小时前
Oracle高可用与容灾解决方案
数据库·oracle
小蒜学长8 小时前
基于springboot 校园餐厅预约点餐微信小程序的设计与实现(代码+数据库+LW)
数据库·spring boot·微信小程序