PropertyPlaceholderConfigurer with Default Values

Written by MikeNereson

July 23rd, 2009 at 9:22 pm

Posted in java

Tagged with ,

With one comment

My current project relies heavily on Spring. We use the PropertyPlaceholderConfigurer so that our application contexts can pull values from the properties files and inject them into our beans. This is all very common. This means when the beans are being created and a value like ${someproperty} shows up, the BeanFactory visits the configured properties files to find the value for someproperty and injects that value into the bean.

The problem arises when a bean is configured using such a placeholder but the requested property is not found. This causes our application to fail to start.

Our solution was to extend PropertyPlaceholderConfigurer to provide default values. These default values are loaded before loading any properties files.  Firs well glance over the java class.

import java.io.IOException;
import java.util.Properties;
import java.util.Map;
import java.util.HashMap;

public class DefaultPropertyPlaceholderConfigurer
        extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
{
  private Map<String, String> startingProperties = new HashMap<String, String>();

  public void setStartingProperties(Map<String, String> startingProperties)
  {
     this.startingProperties = startingProperties;
  }

  public DefaultPropertyPlaceholderConfigurer()
  {
      try
      {
          loadDefaultProperties();
      }
      catch (IOException e)
      {
          logger.warn("failed to load default properties", e);
      }
  }
   private void loadDefaultProperties()
          throws IOException
  {
      final Properties defaultProperties = new Properties();
       for(Map.Entry<String,String> entry : startingProperties.entrySet())
      {
          defaultProperties.put(entry.getKey(), entry.getValue());
      }
       this.setPropertiesArray(new Properties[]{defaultProperties});
  }
}

And the XML bean definition for our new PropertyPlaceholderConfigurer.

  <bean id="propertyConfigurer"
        class="app.factory.config.DefaultPropertyPlaceholderConfigurer">
      <property name="ignoreUnresolvablePlaceholders" value="true" />
      <property name="startingProperties">
          <map>
              <entry key="one.two.three" value="123" />
          </map>
      </property>
      <property name="locations">
          <list>
              <value>/WEB-INF/classes/preferences.properties</value>
          </list>
      </property>
  </bean>

So now, when one.two.three is not included in the properties file preferences.properties the value will be initialized to 123. The value is defined in the properties file, the default value of 123 is overridden with the custom value.

Use Case

Here are the details on our specific use case. We release updates that add new properties, however, in order for the user to run the updates, the application has to start first (it is a web application). If the property is not set then the BeanFactory fails to start the application because it can not resolve the placeholder. So the software can’t start without running the updates first, but the updates can not be run until the application is started.

Now the web application starts and uses the default value, the administrator can run the updates which add the custom values to the preferences.properties file, and the user is prompted to restart the software after the updates are complete. The restart is required to read the new values into the bean.

Oops

While I was writing this post I realized that there was a much simpler way to provide default properties without extending the base class.

Any ideas as to how I could have done it?

Share

Related Posts

Comments

One Response to “PropertyPlaceholderConfigurer with Default Values”

  1. Matias on January 13th, 2011 7:45 am

    By defining multiple locations on the PropertyPlaceholderConfigurer? The first one should contain the defaults, the rest optional values.

    Cheers!

Leave a Reply