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

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..

This question is related to java spring properties

The answer is


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


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.


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.


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/


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to properties

Property 'value' does not exist on type 'EventTarget' How to read data from java properties file using Spring Boot Kotlin - Property initialization using "by lazy" vs. "lateinit" react-router - pass props to handler component Specifying trust store information in spring boot application.properties Can I update a component's props in React.js? Property getters and setters Error in Swift class: Property not initialized at super.init call java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US How to use BeanUtils.copyProperties?