Springboot读取yml文件工具类

洼地云 tuoyidashi.png

除了注解等方式(参考: Spring Boot获取yml文件配置注入Map/List/String等类型 ),还有一种直接读取的实现:

public static Object readProperties(String key) {
        Object propertyValue = null;
        Resource resource = new ClassPathResource("config/application-custom.yml");
        Properties properties;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties = yamlFactory.getObject();
            propertyValue = properties.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return propertyValue;
    }
赞(0)
未经允许禁止转载:优米格 » Springboot读取yml文件工具类

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册