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

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

}
相关推荐
亚控科技21 小时前
超大型数据中心冷源群控升级:自主可控与智能调控的实践
数据库·智慧楼宇·kingscada·亚控科技·信创scada·大型数据中心
naruto_lnq21 小时前
NumPy入门:高性能科学计算的基础
jvm·数据库·python
Apple_羊先森21 小时前
ORACLE数据库巡检SQL脚本--4、检查锁阻塞
数据库·sql·oracle
2301_8223650321 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python
zhangyifang_0091 天前
ClickHouse查询报错:Code: 62. DB::Exception: Max query size exceeded:
数据库·clickhouse
2301_788756061 天前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
uoKent1 天前
MySQL示例数据库
数据库·mysql
麦聪聊数据1 天前
利用SQL2API模式重构微服务中的数据查询层
数据库·sql·低代码·微服务·架构
占疏1 天前
数据库-BRIN 索引
数据库·mysql
Aloudata1 天前
数据工程实践:智能制造企业如何通过NoETL指标平台为数据资产“瘦身”,实现TCO最优?
sql·数据分析·etl·指标平台