Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Spring PropertyPlaceholderConfigurer at the same time

Tags:

java

spring

I have two projects, one of them (Services) includes the second one (Core). I've defined this PropertyPlaceholderConfigurer below within Core project:

<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
         <list>
             <value>classpath:appConfig.properties</value>
         </list>
    </property>
</bean>

And I want to extend the Core placeholder in the upper project, including appConfig.properties and some other ones. The only way I found is to define another different bean (different ID) in the upper level and include the new ones:

<bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
          <list>
                <value>classpath:swagger.properties</value>
          </list>
    </property>
</bean>

But it yields it cannot find appConfig.properties, I guess it's only using one of these PropertyPlaceholderConfigurer at the same time? Is it necesary to specify all resources in the upper propertyConfigurer?:

<bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
          <list>
                <value>classpath:swagger.properties</value>
                <value>classpath:appConfig.properties</value>
          </list>
    </property>
</bean>

Is it posible to extend the Core bean o use both at the same time?

like image 715
Amin Abu-Taleb Avatar asked Sep 09 '13 11:09

Amin Abu-Taleb


2 Answers

You can use multiple at the same time BUT you have to have different names/ids for each else one will override the other. Next to that you need to either use different placeholders (${...}) for each or configure it to ignore unresolved placeholders.

Instead of using the class definition I strongly suggest yo use the namespace (<context:property-placeholder .. /> which saves you some xml. (It will generate beans with unique names so you can have multiple at the same time, make sure to set the ignore-unresolvable attribute to true.

As a last resort you could implement your a BeanDefinitionRegistryPostProcessor which detects all PropertyPlaceHolderConfigurer instances, merges them into one and move all the locations/resources to the merged one. That way you don't have to worry about multiple different placeholders or unresolvable properties.

like image 112
M. Deinum Avatar answered Oct 06 '22 02:10

M. Deinum


try this code

<bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
      <list>
            <value>classpath:swagger.properties</value>
            <value>classpath:appConfig.properties</value>
      </list>
</property>
 <property name="ignoreUnresolvablePlaceholders" value="true"/>

like image 26
Ugur Artun Avatar answered Oct 06 '22 00:10

Ugur Artun