Its better to store the configuration properties in a database table so that it can be managed easily for different environments (dev/QA/prod). This post explains the approach to store and retrieve the configuration properties from database table in a enterprise spring based project.
Configure the below bean definition to the spring context file
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="properties"> <bean class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties"> <constructor-arg> <bean class="org.apache.commons.configuration.DatabaseConfiguration"> <constructor-arg> <ref bean="dbDataSource" /> </constructor-arg> <constructor-arg value="DOMAIN_CONFIG" /> <!-- DB Table --> <constructor-arg value="CONFIG_NAME" /> <!-- DB Key Column --> <constructor-arg value="CONFIG_VALUE" /> <!-- DB Value Column --> </bean> </constructor-arg> </bean> </property> </bean>
The Properties table should be pre-created with the required configuration values
The datasource ref bean propertyDataSource should be configured in the context file –
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="datasource.name" /> </bean>
Now the properties can be referred as shown below –
<int-ip:tcp-connection-factory id="client" type="client" host="${iit.server.host}" port="${iit.server.port}" single-use="true" so-timeout="100000" serializer="connectionSerializeDeserialize" deserializer="connectionSerializeDeserialize"/>
If the same set of properties need to be referred inside the beans as well then we need to make minor adjustments to the context declaration made above. The change will be –
<bean id="domainConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration"> <constructor-arg> <ref bean="dbDataSource" /> </constructor-arg> <constructor-arg value="ERL_3PT_DOMAIN_CONFIG" /> <!-- DB Table --> <constructor-arg value="CONFIG_NAME" /> <!-- DB Key Column --> <constructor-arg value="CONFIG_VALUE" /> <!-- DB Value Column --> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="properties"> <bean class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties"> <constructor-arg> <ref bean="domainConfiguration" /> </constructor-arg> </bean> </property> </bean></pre> <pre>
And a service in java class can be written as follows –
import java.util.Properties; import org.apache.commons.configuration.ConfigurationConverter; import org.apache.commons.configuration.DatabaseConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** * Service to retrieve the properties stored in the DOMAIN_CONFIG table. This service can be autowired in any other service and its getProperty method can be invoked to get the property value. * The getProperty() method makes sure that all the properties from the database are loaded only once throughout the life time of the current application session. * @author akhandel * */ @Service("domainConfigService") public class DomainConfig { @Autowired @Qualifier("domainConfiguration") DatabaseConfiguration domainConfiguration; private Properties properties; public String getProperty(String key) { if (properties == null) { properties = ConfigurationConverter.getProperties(domainConfiguration); } return properties.getProperty(key); } }</pre> <pre>