使用Yaml类读取yml配置文件信息。

    private String getPropertiesValue(String key) {
        
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("application.yml");
        Yaml yaml = new Yaml();

        Map<String, Object> map = yaml.load(inputStream);

        String[] split = key.split("\\.");

        String res = null;

        for (String s : split) {
            Object ob = map.get(s);
            if (!(ob instanceof Map)) {
                res = (String) ob;
            } else {
                map = (Map) ob;
            }
        }
        return res;
    }

调用:

        String key = "spring.profiles.active";
        System.out.println(getPropertiesValue(key));

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注