欢迎光临
我们一直在努力

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;
    }
赞(0) 打赏
未经允许不得转载:Ddmit » Springboot读取yml文件工具类

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册