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.ConfigurableListableBeanFactory; 
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 

public class ExposablePropertyPaceholderConfigurer extends PropertyPlaceholderConfigurer{ 
        private Map<String, String> resolvedProps; 
        protected void processProperties(ConfigurableListableBeanFactory 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(resolvedProps);   
                } 
        public String getValue(String key) {       
                return resolvedProps.get(key);   
                } 
}
(2)
 Configure our ExposablePropertyPaceholderConfigurer in spring config file.
        <bean id="propertyConfig" 
                class="com.dj.spring.framework.ExposablePropertyPaceholderConfigurer"> 
                <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
ExposablePropertyPaceholderConfigurer config= applicationContext.getBean(propertyConfig);
String dbUser=config.getValue("dbuser");


Please leave your comments and suggestions below...

Comments

Popular posts from this blog

Ramoji Film City, Hyderabad, India

Ashtavinayak Temples | 8 Ganpati Temples of Maharashtra | Details Travel Reviews

Quick Refresh : Hibernate