前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot实战之读取自定义配置文件

springboot实战之读取自定义配置文件

作者头像
lyb-geek
发布2019-08-06 17:03:06
7.8K0
发布2019-08-06 17:03:06
举报
文章被收录于专栏:Linyb极客之路Linyb极客之路

前言

我们在开发springboot的项目过程中,为了不把所有的配置信息全都写在application.(yml/properties)中,我们需要自定义配置文件比如common.properties,那么问题来了,我们要如何读取common.properties里面的内容。

也许有小伙伴会说那还不简单,直接写个读取配置文件工具类来读取不就行了。这样确实可以满足需求,但有没有更优雅的读取方法,比如用springboot提供的@Value注解就可以取到配置信息,而非用PropertiesUtil.getProperty("abc")方式来获取。

答案是有的,以下介绍两种方案实现用@value来获取自定义配置文件

第一种方案,利用@PropertySource注解来实现

@PropertySource可以用来加载指定的配置文件,默认它只能加载*.properties文件,不能加载诸如yaml等文件。

@PropertySource相关属性介绍

  • value:指明加载配置文件的路径。
  • ignoreResourceNotFound:指定的配置文件不存在是否报错,默认是false。当设置为 true 时,若该文件不存在,程序不会报错。实际项目开发中,最好设置 ignoreResourceNotFound 为 false。
  • encoding:指定读取属性文件所使用的编码,我们通常使用的是UTF-8。

示例

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Configuration
@PropertySource(value = {"classpath:common.properties"},ignoreResourceNotFound=false,encoding="UTF-8")
@ConfigurationProperties(prefix = "author")
public class Author {
    private String name;
    private String job;
    private String sex;
}

有小伙伴也许发现示例上的@ConfigurationProperties注解了。当我们使用@Value需要注入的值较多时,代码就会显得冗余。我们可以使用@ConfigurationProperties 中的 prefix 用来指明我们配置文件中需要注入信息的前缀

前边提到了用@PropertySource只能加载*.properties文件,但如果我们项目的配置文件不是*.properties这种类型,而是其他类型,诸如yaml,此时我们可以通过实现PropertySourceFactory接口,重写createPropertySource方法,就能实现用@PropertySource也能加载yaml等类型文件。

示例代码

代码语言:javascript
复制
public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String sourceName, EncodedResource resource) throws IOException {
        Properties propertiesFromYaml = loadYaml(resource);
        if(StringUtils.isBlank(sourceName)){
            sourceName =  resource.getResource().getFilename();;
        }

        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    }
    private Properties loadYaml(EncodedResource resource) throws FileNotFoundException {
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource.getResource());
            factory.afterPropertiesSet();
            return factory.getObject();
        } catch (IllegalStateException e) {
            // for ignoreResourceNotFound
            Throwable cause = e.getCause();
            if (cause instanceof FileNotFoundException)
                throw (FileNotFoundException) e.getCause();
            throw e;
        }
    }
}
代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Configuration
@PropertySource(factory = YamlPropertySourceFactory.class,value = {"classpath:user.yml"},ignoreResourceNotFound=false,encoding="UTF-8")
@ConfigurationProperties(prefix = "user")
public class User {
    private String username;

    private String password;
}

第二种方案,使用EnvironmentPostProcessor加载自定义配置文件

其实现流程如下:

1、实现EnvironmentPostProcessor接口,重写postProcessEnvironment方法

代码语言:javascript
复制
@Slf4j
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

        Properties properties = new Properties();

        try {
            properties.load(new InputStreamReader(CustomEnvironmentPostProcessor.class.getClassLoader().getResourceAsStream("custom.properties"),"UTF-8"));

            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("custom",properties);
            environment.getPropertySources().addLast(propertiesPropertySource);
        } catch (IOException e) {
          log.error(e.getMessage(),e);
        }

    }
}

2、在META-INF下创建spring.factories

代码语言:javascript
复制
spring.factories文件内容如下:
org.springframework.boot.env.EnvironmentPostProcessor=com.github.lybgeek.env.CustomEnvironmentPostProcessor

1、2步骤实现完后,就可以在代码中直接用@Value的方式获取自定义配置文件内容了

总结

读取的自定义配置文件内容的实现方法有多种多样,除了上面的方法,还可以在以-jar方式启动时,执行形如下命令

java -jar project.jar --spring.config.location=classpath:/config/custom.yml

也能实现。还可以干脆自定义配置文件都以application-*为前缀,比如application-custom,然后在application.properties,使用spring.profiles.include=custom或者spring.profiles.active=custom也可以实现

demo链接

https://github.com/lyb-geek/springboot-learning/tree/master/springboot-outside-config

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 第一种方案,利用@PropertySource注解来实现
  • 第二种方案,使用EnvironmentPostProcessor加载自定义配置文件
  • 总结
  • demo链接
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档