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

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

}
相关推荐
ttthe_MOon几秒前
Redis Cluster集群模式和各种常见问题
数据库·redis·缓存
小鸡脚来咯3 分钟前
MySQL InnoDB内存结构,增删改查时怎么运行的
数据库·mysql
杨了个杨898213 分钟前
PostgreSQL(pgSQL)常用操作
数据库·postgresql·oracle
蝈蝈(GuoGuo)16 分钟前
SQL Server 中指定范围分页取数详解
数据库
慕白Lee20 分钟前
【PostgreSQL】日常总结
数据库·postgresql
sc.溯琛25 分钟前
MySQL 视图实战:简化查询与数据安全管控指南
数据库
风月歌27 分钟前
小程序项目之校园二手交易平台小程序源代码(源码+文档)
java·数据库·mysql·小程序·毕业设计·源码
西格电力科技33 分钟前
绿电直连架构适配技术的发展趋势
大数据·服务器·数据库·架构·能源
计算机毕设VX:Fegn089538 分钟前
计算机毕业设计|基于springboot + vue汽车销售系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·汽车·课程设计
@小白向前冲1 小时前
数据库创表(方便自己查看)
数据库·mysql