Thursday June 23, 2005
Externalizing Configuration with Spring
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>
Posted on Jun 23, 2005 at 22:31 (MET) | Permalink | 8 comments