Search code examples
springapache-commons-config

Use Commons Configuration 2 as PropertySource in Spring


Commons Configuration 2.0 is out. With Commons Configuration 1.0 there was a Spring modules factory bean (org.springmodules.commons.configuration.CommonsConfigurationFactoryBean) that allows direct usage of Commons Configuration with Spring's PropertyPlaceholderConfigurer. As this is no longer maintained, the question is how to do this with Commons Configuration 2.0.

Of course it should be possible to copy the existing Spring modules source code to the project and migrate it to 2.0. I know Spring offers YAML, but it should be still Commons Configuration (the existing XML configuration files should not be affected).


Solution

  • I contributed a PropertySource for Commons Configuration, it's part of version >=2.1: org.apache.commons.configuration2.spring.ConfigurationPropertySource

    Use it for example in an extended 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);
       }
    }
    

    The code would be simpler if this issue is fixed: https://jira.spring.io/browse/SPR-9631

    See also: http://mail-archives.apache.org/mod_mbox/commons-user/201604.mbox/%[email protected]%3E