在Spring

时间:2016-04-01 09:37:03

标签: spring apache-commons-config

Commons Configuration 2.0已经发布。使用Commons Configuration 1.0,有一个Spring模块工厂bean (org.springmodules.commons.configuration.CommonsConfigurationFactoryBean)允许直接使用Spring的Commons配置 PropertyPlaceholderConfigurer。由于这不再维持,所以 问题是如何使用Commons Configuration 2.0。

当然应该可以复制现有的Spring模块 项目的源代码并将其迁移到2.0。我知道Spring提供YAML,但它应该仍然是Commons Configuration(现有的XML配置文件不应该受到影响)。

1 个答案:

答案 0 :(得分:2)

我为Commons Configuration贡献了一个PropertySource,它是版本> = 2.1的一部分:org.apache.commons.configuration2.spring.ConfigurationPropertySource

例如在扩展的PropertySourcesPlaceholderConfigurer中使用它:

public class ApacheCommonsConfigPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {

   public ApacheCommonsConfigPlaceholderConfigurer(Configuration configuration) {
     ConfigurationPropertySource apacheCommonsConfigPropertySource =
        new ConfigurationPropertySource(configuration.getClass().getName(), configuration);
     MutablePropertySources propertySources = new MutablePropertySources();
     propertySources.addLast(apacheCommonsConfigPropertySource);
     setPropertySources(propertySources);
   }
}

如果修复此问题,代码会更简单:https://jira.spring.io/browse/SPR-9631

另请参阅:http://mail-archives.apache.org/mod_mbox/commons-user/201604.mbox/%3C44F558D5-0A26-4711-9658-39BD9052D751@capitalone.com%3E