Instantiating custom PropertySourcesPlaceholderConfigurer from spring context

Posted by mmona on Stack Overflow See other posts from Stack Overflow or by mmona
Published on 2014-01-23T11:05:57Z Indexed on 2014/08/21 22:20 UTC
Read the original article Hit count: 159

Filed under:
|

I want to define a custom PropertySourcesPlaceholderConfigurer in spring context xml. I want to use there multiple PropertySources, so that I can load part of the configuration from several property files and provide other part dynamically by my custom PropertySource implementation. The advantage is that it should be then easy to adjust the order of loading these property sources just by making modifications to the xml spring configuration.

And here I run into a problem: how to define an arbitrary list of PropertySources and inject it into PropertySourcesPlaceholderConfigurer, so that it uses the sources defined by me?

Seems to be a basic thing that should be provided by spring, but since yesterday I cannot find a way to do it. Using namespace would enable me to load several property files, but I also need to define the id of the PropertySourcesPlaceholderConfigurer (as other projects refer to it), and also I want to use my custom implementation. That is why I am defining the bean explicitly and not using the namespace.

The most intuitive way would be to inject a list of PropertySources into PropertySourcesPlaceholderConfigurer like this:

<bean id="applicationPropertyPlaceholderConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true" />     
    <property name="order" value="0"/>
    <property name="propertySources">
        <list>
             <!-- my PropertySource objects -->
        </list>
    </property> 
</bean>

but unfortunately propertySources is of type PropertySources and does not accept a list. The PropertySources interface has one and only implementor which is MutablePropertySources, which indeed stores list of PropertySource objects, but has no constructor nor setter through which I can inject this list. It only has add*(PropertySource) methods.

The only workaround I see now is to implement my own PropertySources class, extending MutablePropertySources, which would accept list of PropertySource objects on creation and manually add it via using add*(PropertySource) method. But why so much workaround would be needed to provide something that I thought was supposed to be the main reason of introducing the PropertySources (having flexible configuration manageable from spring configuration level).

Please clarify what am I getting wrong :)

© Stack Overflow or respective owner

Related posts about java

Related posts about spring