commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <>
Subject [Jakarta-commons Wiki] Update of "Managing Configurations using Spring" by JoshuaNichols
Date Fri, 24 Mar 2006 16:25:31 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by JoshuaNichols:

The comment on the change is:
Adding entry about commons-configuration + spring, as requested in bug #39068

New page:
If you use commons-configuration in several places within your application, particularly when
you reuse the same files backing the configurations, you will may notice that you have the
same code chunks sprinkled around your code. Being the good Java developer you are, you want
to reduce copy-and-pasted code, and refactor it to provide a standard way at getting a Configuration
you build. Being the good Java developer, you also are using the wonderful Spring framework.

Without further ado, here is a way of using Spring to manage your configurations. In this
example, we want to use a few PropertiesConfigurations, as well as a MapConfiguration, and
use these together to build a CompositeConfiguration.

In your spring context, you would have the following beans:

    <bean id="buildProperties" class="org.apache.commons.configuration.PropertiesConfiguration">
        <constructor-arg index="0"><value></value></constructor-arg>
    <bean id="projectProperties" class="org.apache.commons.configuration.PropertiesConfiguration">
        <constructor-arg index="0"><value></value></constructor-arg>
    <bean id="defaultProperties" class="org.apache.commons.configuration.MapConfiguration">
    	<constructor-arg index="0">
                <entry key="sync.facade.printService" value="mockPrintService"/>
                <entry key="sync.facade.fulfillService" value="mockFulfillService"/>
                <entry key="sync.facade.userService" value="mockUserService"/>
                <entry key="acegi.authenticationDao" value="mockAuthenticationDao"/>

    <bean id="compositeProperties" class="org.apache.commons.configuration.CompositeConfiguration">
        <constructor-arg index="0">
            <!-- The order that these beans are defined is important when properties are
defined in multiple Configurations -->
            	<ref bean="defaultProperties"/>
                <ref bean="projectProperties"/>
                <ref bean="buildProperties"/>

So now, you can inject this configuration into your other beans, or get at the CompositeConfiguration
directly using an ApplicationContext.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message