Springboot读取yml文件工具类

除了注解等方式(参考: 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;
    }
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容