Spring : Reading Properties file
Spring 2.5 : Reading Properties file:
PropertyPlaceholderConfigurer: The PropertyPlaceholderConfigurer is used to externalize property values from a BeanFactory definition, into another separate file in the standard Java Properties format. This is useful to allow the person deploying an application to customize environment-specific properties (for example database URLs, usernames and passwords), without the complexity or risk of modifying the main XML definition file or files for the container.
<bean class="org.springframework. beans.factory.config. PropertyPlaceholderConfigurer" >
<property name="location">
<value>database.properties</ value>
</property>
</bean>
Getting Property Value in Spring Configuration: Use ${propertyKey}
<bean id="dataSource" class="org.springframework. jdbc.datasource. DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName} " />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
Getting Property Value in Java Class:
(1) We need to extend PropertyPlaceholderConfigurer and implement processProperties method.Where we can create a map of properties.
package com.dj.spring.framework;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans. BeansException;
import org.springframework.beans. factory.config. ConfigurableListableBeanFactor y;
import org.springframework.beans. factory.config. PropertyPlaceholderConfigurer;
public class ExposablePropertyPaceholderCon figurer extends PropertyPlaceholderConfigurer{
private Map<String, String> resolvedProps;
protected void processProperties( ConfigurableListableBeanFactor y beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties( beanFactoryToProcess, props);
resolvedProps = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
resolvedProps.put(keyStr, parseStringValue(props. getProperty(keyStr), props, newHashSet()));
}
}
public Map<String, String> getResolvedProps() {
return Collections.unmodifiableMap(re solvedProps);
}
public String getValue(String key) {
return resolvedProps.get(key);
}
}
(2) Configure our ExposablePropertyPaceholderCon figurer in spring config file.
<bean id="propertyConfig"
class="com.dj.spring. framework. ExposablePropertyPaceholderCon figurer">
<property name="locations">
<list>
<value>application-db. properties</value>
<value>application-sql. properties</value>
</list>
</property>
</bean>
(3) application-sql.properites
dbuser=test
dbpassword=xxx
(4) Accesssing Property in java class
ExposablePropertyPaceholderCon figurer config= applicationContext.getBean(propertyConfig);
String dbUser=config.getValue("dbuser");
PropertyPlaceholderConfigurer: The PropertyPlaceholderConfigurer is used to externalize property values from a BeanFactory definition, into another separate file in the standard Java Properties format. This is useful to allow the person deploying an application to customize environment-specific properties (for example database URLs, usernames and passwords), without the complexity or risk of modifying the main XML definition file or files for the container.
<bean class="org.springframework.
<property name="location">
<value>database.properties</
</property>
</bean>
Getting Property Value in Spring Configuration: Use ${propertyKey}
<bean id="dataSource" class="org.springframework.
<property name="driverClassName" value="${jdbc.driverClassName}
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
Getting Property Value in Java Class:
(1) We need to extend PropertyPlaceholderConfigurer and implement processProperties method.Where we can create a map of properties.
package com.dj.spring.framework;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.
import org.springframework.beans.
import org.springframework.beans.
public class ExposablePropertyPaceholderCon
private Map<String, String> resolvedProps;
protected void processProperties(
super.processProperties(
resolvedProps = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
resolvedProps.put(keyStr, parseStringValue(props.
}
}
public Map<String, String> getResolvedProps() {
return Collections.unmodifiableMap(re
}
public String getValue(String key) {
return resolvedProps.get(key);
}
}
(2) Configure our ExposablePropertyPaceholderCon
<bean id="propertyConfig"
class="com.dj.spring.
<property name="locations">
<list>
<value>application-db.
<value>application-sql.
</list>
</property>
</bean>
(3) application-sql.properites
dbuser=test
dbpassword=xxx
(4) Accesssing Property in java class
ExposablePropertyPaceholderCon
String dbUser=config.getValue("dbuser");
Please leave your comments and suggestions below...
Comments
Post a Comment