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

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

}
相关推荐
小伍_Five1 小时前
使用Java操作Neo4j数据库
大数据·数据库·nosql数据库·neo4j
码农不惑1 小时前
Django的定制以及admin
数据库·python·django·sqlite
Elastic 中国社区官方博客2 小时前
Elasticsearch 向量数据库,原生支持 Google Cloud Vertex AI 平台
大数据·数据库·人工智能·elasticsearch·搜索引擎·语言模型·自然语言处理
一个数据大开发4 小时前
如何将excel数据快速导入数据库
数据库·excel
一介草民丶6 小时前
Mysql | 主从复制的工作机制
数据库·mysql·oracle
酱学编程9 小时前
redis 延迟双删
数据库·redis·缓存
xujiangyan_11 小时前
MySQL的半同步模式
数据库·git·mysql
飞翔沫沫情11 小时前
《MySQL 5.7.44审计合规实践:插件集成与日志分割自动化方案》
数据库·mysql·mysql审计
MXsoft61811 小时前
云原生运维在 2025 年的发展蓝图
运维·服务器·数据库
不辉放弃12 小时前
SQL 主键(Primary Key)
数据库·sql·oracle