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

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

}
相关推荐
悟能不能悟11 分钟前
mysql的not exists走索引吗
数据库·mysql
明月与玄武12 分钟前
Jmeter -- JDBC驱动连接数据库超详细指南
数据库·jmeter·配置jdbc连接
专注VB编程开发20年13 分钟前
VB.NET关于接口实现与简化设计的分析,封装其他类
java·前端·数据库
vvilkim18 分钟前
Redis持久化机制详解:保障数据安全的关键策略
数据库·redis·缓存
cooldream200925 分钟前
信息安全的基石:深入理解五大核心安全服务
数据库·安全·系统架构师
大数据魔法师36 分钟前
Redis(三) - 使用Java操作Redis详解
java·数据库·redis
noravinsc39 分钟前
e.g. ‘django.db.models.BigAutoField‘.
数据库·django
IT光1 小时前
Redis 五种类型基础操作(redis-cli + Spring Data Redis)
java·数据库·redis·spring·缓存
TiDB 社区干货传送门2 小时前
从40秒到11毫秒:TiDB环境下一次SQL深潜优化实战
数据库·sql·tidb
IP管家2 小时前
企业级IP代理解决方案:负载均衡与API接口集成实践
服务器·网络·数据库·网络协议·tcp/ip·容器·负载均衡