SpringBoot配置文件@PropertySource 同时支持properties文件与yaml(yml)

Yaml(yml)最近比较流行的配置文件,相对properties,配置文件结构更清晰简洁.前段时间项目需要引入的配置,于是想用yml文件来增加新的属性配置,新增属性放在application.yml中是没问题的,但是放其他文件中,然后通过@PropertySource 引入时,却出现了问题,所有.yml中的参数配置全部读取无效,properties文件是正常的,后来在stackoverflow上看到@PropertySource中存在factory参数,通过配置factory参数可以达到我们想要的效果。
@PropertySource factory属性的factory默认配置是Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;, 我们再看下PropertySourceFactory的源码就可知道了,

/**
 * Strategy interface for creating resource-based {@link PropertySource} wrappers.
 *
 * @author Juergen Hoeller
 * @since 4.3
 * @see DefaultPropertySourceFactory
 */
public interface PropertySourceFactory {

	/**
	 * Create a {@link PropertySource} that wraps the given resource.
	 * @param name the name of the property source
	 * @param resource the resource (potentially encoded) to wrap
	 * @return the new {@link PropertySource} (never {@code null})
	 * @throws IOException if resource resolution failed
	 */
	PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException;

}

默认实现是DefaultPropertySourceFactory,

/**
 * The default implementation for {@link PropertySourceFactory},
 * wrapping every resource in a {@link ResourcePropertySource}.
 *
 * @author Juergen Hoeller
 * @since 4.3
 * @see PropertySourceFactory
 * @see ResourcePropertySource
 */
public class DefaultPropertySourceFactory implements PropertySourceFactory {

	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
	}

}

我们所有做的只需继承DefaultPropertySourceFactory,然后对createPropertySource作下微调,就可以支持yaml了.

 public class MixPropertySourceFactory extends DefaultPropertySourceFactory {

  @Override
  public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
    String sourceName = name != null ? name : resource.getResource().getFilename();
    if (!resource.getResource().exists()) {
      return new PropertiesPropertySource(sourceName, new Properties());
    } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
      Properties propertiesFromYaml = loadYml(resource);
      return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    } else {
      return super.createPropertySource(name, resource);
    }
  }

  private Properties loadYml(EncodedResource resource) throws IOException {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(resource.getResource());
    factory.afterPropertiesSet();
    return factory.getObject();
  }
}

这里做了个简单的文件后缀判断,如果是以.yml或.yaml结尾,则通过YamlPropertiesFactoryBean加载,其他情况则采用默认方式加载.如果大家需要支持json或xml等其他格式,也可在这里自动加入策略处理.
最后在SpringApplication上加入配置

@PropertySource(value = {
   "a.properties",
   "b.yml"
}, factory = MixPropertySourceFactory.class)

就可以了

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值