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:
http://wiki.apache.org/jakarta-commons/Managing_Configurations_using_Spring

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>build.properties</value></constructor-arg>
    </bean>
    
    <bean id="projectProperties" 
class="org.apache.commons.configuration.PropertiesConfiguration">
        <constructor-arg 
index="0"><value>project.properties</value></constructor-arg>
    </bean>
    
    <bean id="defaultProperties" 
class="org.apache.commons.configuration.MapConfiguration">
        <constructor-arg index="0">
                <map>
                <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"/>
                </map>
        </constructor-arg>
    </bean>

    <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 -->
            <list>
                <ref bean="defaultProperties"/>
                <ref bean="projectProperties"/>
                <ref bean="buildProperties"/>
            </list>
        </constructor-arg>
    </bean>
}}}

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

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to