Loading Multiple Java Property Files Using Spring
Introduction
I had to load properties from two different property files in a Java Spring project. Since I had not done that before and I'm a Spring newbie, I did some research in the Spring documentation. I found out that Spring provides a very easy to use methodology to load properties from multiple property files and then inject the property values into your Spring managed beans. This article will explain what I learned and provide an example Spring Java application you can download.
Spring Context Schema
The context schema was introduced in Spring 2.5. It provides several utilities for setting up beans used to configure other beans. To use the context schema you need to have the following in your Spring application context.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
...
</beans>
Property Placeholder Tag
One of the tags in the context namespace is the property-placeholder. This tag sets up a PropertyPlaceholderConfigurer. The property placeholder tag has a location attribute where you can specify one or more Spring resource locations, each separated by a comma.
Example Application
You can download the example application here. The example is an archived Java application created using Eclipse 3.5 (with the Maven 2 plugin) and uses Maven to manage dependencies (for more information about Maven see the references section below). If you use Eclipse 3.5 you can import the archive into your workspace. If you don't use Eclipse 3.5 you can unzip the archive and examine the source code using any text editor.
Here is how I used the property placeholder tag in the example application (see file application-context.xml).
<context:property-placeholder location="classpath:edu/ku/it/si/propertiesexample/properties/db.properties,classpath:edu/ku/it/si/propertiesexample/properties/email.properties"/>
In the location attribute I specified two class path resource locations for the two property files. When the Spring application context is loaded, Spring will read each property in both properties files and then make each property available to be referenced in the application context by using the format ${propertyname}.
For example one of the properties defined in the db.properties file is db.username=contactsDB. So to refer to the value of this property I use ${db.username} (see the dataSource bean configuration in application-context.xml).
The example application is setup with one JUnit test that can be run to check that the beans configured by Spring have received the correct values from the two property files. You can run the test using mvn test from a command window (terminal) by navigating to the application's root folder (where pom.xml is located) and typing mvn test. You can also run the test from within Eclipse 3.5 by right clicking on TestProperties.java and selecting Run As – JUnit Test.
Summary
Spring 2.5 makes it even easier to use property files, whether your application has one or many. Consult the references below for more information.
References
- The Context Schema, Spring 2.5.6 Documentation, http://static.springsource.org/spring/docs/2.5.x/reference/xsd-config.html#xsd-config-body-schemas-context
- PropertyPlaceHolderConfigurer, Spring 2.5.X Documentation, http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer
- Maven: The Definitive Guide, http://www.sonatype.com/books/maven-book/reference/public-book.html
- Developing with Eclipse and Maven, http://www.sonatype.com/books/m2eclipse-book/reference/index.html


There are no comments for this entry.
[Add Comment]