1) Define the properties beans in spring xml file (typically ApplicationConfiguration.xml file)
as follows:
<!-- Apache Commons Configuration Composite configuration -->2) Define a bean with the getter and setter that returns/accepts an array of org.apache.commons.configuration.Configuration class. To make it easier for the rest of the code to get the configuration easy, you may want to add a utility method in there to return the combined Configutaion. Here is an example:
<bean id="configurations"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<property name="configurations">
<list>
<bean class="org.apache.commons.configuration.PropertiesConfiguration">
<constructor-arg type="java.net.URL"
value="classpath:myconfiguration.properties" />
<property name="reloadingStrategy">
<bean class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"/>
</property>
</bean>
<bean class="org.apache.commons.configuration.SystemConfiguration"/>
</list>
</property>
<!-- define configuration as a set of spring resources -->
</bean>
<bean id="configuration" class="org.apache.commons.configuration.Configuration" factory-bean="&configurations" factory-method="getConfigurations"/>
private Configuration configs[] ;3) Define the bean properties in the spring xml file. I suggest, that you may want to define the above method in a base class of all your beans and define it as an abstract bean. This will enable you to use the properties in all your beans without having to define the properties in every bean. Here is an example:
private ConfigurationBuilder cfgBuilder ;
public Configuration[] getConfigs() {
return configs;
}
public void setConfigs(Configuration[] configs) {
this.configs = configs;
cfgBuilder = new ConfigurationBuilder();
for (Configuration cgf: configs) {
cfgBuilder.addConfiguration(cgf);
}
}
public Configuration getConfig() {
return cfgBuilder.getConfiguration();
}
<bean id="baseActionBean" class="com.my.company.BaseAction" abstract="true" >4) Add the necessary libraries if you do not have them already. Here is the list of jars you will need.
<property name="configs" ref="configuration" />
</bean>
<bean id="logonClass" class="com.my.company.UsefulAction" parent="baseActionBean">
</bean>
commons-lang-*.jarWith these changes, you should be able to use the properties in your beans with a code as simple as getConfig().getString("propertyKey")
commons-configuration*.jar
spring-modules-jakarta-commons*.jar
No comments:
Post a Comment