Customising Spring Property File Loading

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author

Aim

To be able to intervene when Spring loads a property from a file

  1. Create the property reader class
public class MyPropertyConversionClass extends PropertyPlaceholderConfigurer {
 
    protected String convertPropertyValue(String arg0) {
        // convert / decrypt the value and return / delegate to the super class
        return super.convertPropertyValue(arg0);
    }
}
  1. Refer to it in the Spring config (xml) file as follows
<bean id="myPropertyConfigurer" class="net.rp.config.RpPropertyPlaceholderConfigurer">
    <property name="location" value="name-of-property-file.properties"/>
    <property name="placeholderPrefix" value="$myprefix{"/>
  </bean>

Now spring will call the convertPropertyValue() method for everywhere it finds the placeholder myprefix{ and then look for the key-to-look-for-in-property-file in the name-of-property-file.properties.

<property name="someSpecialProperty" value="$myprefix{key-to-look-for-in-property-file}" />

Spring Reading Properties from JNDI

A similar 'change from default property file loading' also allows to load property files from JNDI - For example we can set them in an app server console for ease of administration (easier than modifying Spring config and rebuilding). The example below shows us doing this for a Data Pool name.

<bean id="jndi:ServicePropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="jndi.properties"/>
    <property name="placeholderPrefix" value="$jndi{"/>
  </bean>
 
  <bean id="dataSourcePool" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="$jndi{db.pool.olbm}"/>
  </bean>

Other Spring Related Articles

Spring MVC | Maven Information | Hibernate | Sending Web Email from Spring | Spring Pointer to Log4j Configuration File | Cron Timer functionality using Spring and Quartz | Customising Spring Property File Loading

Personal tools