Using multiple property files (via PropertyPlaceholderConfigurer) in multiple projects/modules

JavaSpringProperties

Java Problem Overview


We are currently writing an application which is split into multiple projects/modules. For example, let's take the following modules:

  • myApp-DAO
  • myApp-jabber

Each module has its own Spring context xml file. For the DAO module I have a PropertyPlaceholderConfigurer which reads a property file with the necessary db connection parameters. In the jabber module I also have a PropertyPlaceHolderConfigurer for the jabber connection properties.

Now comes the main application which includes myApp-DAO and myApp-jabber. It reads all the context files and starts one big Spring context. Unfortunately it seems like there can only be one PropertyPlaceholderConfigurer per context, so whichever module gets loaded first is able to read it's connection parameters. The other one throws an exception with an error like "Could not resolve placeholder 'jabber.host'"

I kind of understand what the problem is, but I don't really know a solution - or the best practice for my usecase.

How would I configure each module so that each one is able to load its own property file? Right now I've moved the PropertyPlaceHolderConfigurer out of the seperate context files and merged them into the main application's context (loading all property files with a single PropertyPlaceHolderConfigurer). This sucks though, because now everyone who uses the dao module has to know, that they need a PropertyPlaceHolderConfigurer in their context .. also the integration tests in the dao module fail etc.

I'm curious to hear about solutions/ideas from the stackoverflow community..

Java Solutions


Solution 1 - Java

If you ensure that every place holder, in each of the contexts involved, is ignoring unresolvable keys then both of these approaches work. For example:

<context:property-placeholder
location="classpath:dao.properties,
          classpath:services.properties,
          classpath:user.properties"
ignore-unresolvable="true"/>

or

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dao.properties</value>
                <value>classpath:services.properties</value>
                <value>classpath:user.properties</value>
            </list>
        </property> 
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

Solution 2 - Java

I know that this is an old question, but the ignore-unresolvable property was not working for me and I didn't know why.

The problem was that I needed an external resource (something like location="file:${CATALINA_HOME}/conf/db-override.properties") and the ignore-unresolvable="true" does not do the job in this case.

What one needs to do for ignoring a missing external resource is:

ignore-resource-not-found="true"

Just in case anyone else bumps into this.

Solution 3 - Java

You can have multiple <context:property-placeholder /> elements instead of explicitly declaring multiple PropertiesPlaceholderConfigurer beans.

Solution 4 - Java

The PropertiesPlaceholderConfigurer bean has an alternative property called "propertiesArray". Use this instead of the "properties" property, and configure it with an <array> of property references.

Solution 5 - Java

I tried the solution below, it works on my machine.

<context:property-placeholder location="classpath*:connection.properties" ignore-unresolvable="true" order="1" />

<context:property-placeholder location="classpath*:general.properties" order="2"/>

> In case multiple elements are present in the > Spring context, there are a few best practices that should be > followed: > > the order attribute needs to be specified to fix the order in which > these are processed by Spring all property placeholders minus the last > one (highest order) should have ignore-unresolvable=”true” to allow > the resolution mechanism to pass to others in the context without > throwing an exception

source: http://www.baeldung.com/2012/02/06/properties-with-spring/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionblack666View Question on Stackoverflow
Solution 1 - JavaTim HennekeyView Answer on Stackoverflow
Solution 2 - JavaRaul ReneView Answer on Stackoverflow
Solution 3 - JavaearldouglasView Answer on Stackoverflow
Solution 4 - JavaStephen CView Answer on Stackoverflow
Solution 5 - JavaonurbaysanView Answer on Stackoverflow