Externalizing Configuration with Spring
Filed under: Java, Engineering
Spring has basically two classes to externalize configuration parameters from an application context to a property file. Both classes are ok to use, but it takes a couple of minutes to fully understand both approaches. After having gone through this twice, i thought i write my experiences down for next time…
- PropertyPlaceholderConfigurer - this class allows to replace placeholders in the bean definitions file with parameters from a properties file. E.g.:
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>${database.datasource.jndi_name}</value>
</property>
</bean>
In this case ${database.datasource.jndi_name} will be replaced with the according property value in a property file, such as this:
# Name of datasource in jndi
database.datasource.jndi_name=java:comp/env/jdbc/myds
- PropertyOverrideConfigurer - this class allows to override bean properties in the bean definition, so it is possible to override actual values in the bean definition. The property names in this case need to match the schema beanName.beanProperty. The example above would look like this:
<bean id="dataSource"
class="org.springframework.jndi.
JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myds</value>
</property>
</bean>
So, the default value (java:comp/env/jdbc/myds) are already in the bean definition. If the default value needs to be overridden, the properties file would look like this:
# Name of datasource in jndi
dataSource.jndiName=java:comp/env/jdbc/myds
Both classes can be configured programatically and declaratively, while the latter is probably better. It’s quite easy:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:my.properties</value>
</list>
</property>
</bean>
I decided to go with the PropertyPlaceholderConfigurer, because
- it is more visible in the bean definition file what is going to be overridden,
- i didn’t find a way to override properties of type java.util.Property when using the PropertyOverrideConfigurer ,
- and it is still possible to define default values (see below).
Overriding property values with the the PropertyPlaceholderConfigurer is easy, too. Just define default values in the bean definition for your PropertyPlaceholderConfigurer and then have an external properties file, which the overrides these properties if needed. My property file looks like this:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">
<!-- Default properties -->
<property name="properties">
<props>
<prop key="database.datasource.jndi_name">
java:comp/env/jdbc/owms
</prop>
</props>
</property>
<property name="locations">
<list>
<value>classpath:my.properties</value>
</list>
</property>
</bean>
Jun 23, 2005 at 22:31 | Permalink
8 Comments
1. sebastian | July 28th, 2005 at 10:28 am
very helpful;
however, the correct syntax of the element is
:
valueAsString
:
instead of
valueAsString
or did this change in a newer version of spring? i’m using version 1.2.2;
2. sebastian | July 28th, 2005 at 10:31 am
the braces aren’t being displayed (of course)… what i meant was:
the syntax is
valueAsString
instead of
valueAsString
3. Stefan | July 28th, 2005 at 10:35 pm
Hi Sebastian, i’m not sure which element you mean. If i search the Spring doco or mailinglist, there are no results for “valueAsString”. Can you explain?
4. Stefan | August 3rd, 2005 at 12:41 am
Sebastian gave me a hint. Thanks for that!
(in fact, the sample was not as it is my actual project’s app context)
—–Original Message—–
From: sebastian [mailto:xxxxxx@xxxxx.xxxx]
Sent: Tuesday, August 02, 2005 10:07 AM
To: Stefan Kleineikenscheidt
Subject: Re: Spring Configuration
the correct syntax for the element in spring reads
<props><prop key="theKey">theValue</prop>
</props>
instead of
<props><prop key="theKey">
<value>theValue</value>
</prop>
</props>
maybe this changed only in a newer version of spring and your syntax
still works with an older version…
anyways, could you kindly remove the two comments i have posted, they
are rather confusing (with the braces not being displayed)…
brgrds,
sebastian
5. j. betancourt | August 6th, 2005 at 6:20 pm
Maybe more correct is to say that a properties place holder configurer uses a Properties to replace stuff. Does not have to come from a properties file.
For example, you can set the “properties” property to a PropertiesFactoryBean in the context.
6. Stefan | August 11th, 2005 at 12:21 am
Hi J., that’s exactly my point, the default properties are in the context definition, while an external file (editable by administrators and the like) can be used to override the default values.
7. Wunschdenken » Blog&hellip | December 24th, 2006 at 2:18 pm
[…] In out-of-the-box Spring, there are two ways to do this with a PropertyPlaceholderConfigurer: either put a list of default property bindings into the XML file itself, or put them into another Properties file, which may be contained as a resource in the jar file. Stefan Kleineikenscheidt has a good description of this approach. […]
8. Murugan | March 30th, 2009 at 5:12 pm
Hey,
I am new to spring I would like to get the value in from a properties file in (for e.g. WEB-INF/env.properties) in a jsp page (I would like to display what environment it is) .
How can I do it?..a brief approach can help me..Thanks…
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed