Application Configuration properties simplified by Spring

The combination of PropertyPlaceholderConfigurer and PropertyOverrideConfigurer classes in Spring Beans API have been invaluable for me to simplify configuration properties for a few of applications I have worked-on recently. The placeholder configurer helps us to share the properties across various pieces of the application using “placeholders”.

As recommended in the Spring reference manual, I usually setup a “default” configuration file ( file that is packaged within one of the main Jar files. This keeps the actual (overriding production) properties file very short as you only override the properties that are different than the default properties file.

Sometimes, a simple loading of the configuration properties may not be enough. There might be a need for post-processing the properties using some custom code.

Certain properties may have to be:

  • Selectively disabled/enabled
  • Translated based on the values of others
  • Set & be available within the application (in the code that is not wired-up in Spring XML)

Spring does have hooks for some of these to decrypt/encrypt properties, intercept the processing of each of the String values, etc. This means writing a new class that overrides the natural behavior of these Configurer classes.

Both of these Configurer classes are implemented as bean factory post-processors. So, if ApplicationContext is used instead of BeanFactory, these should automagically work. If you are using BeanFactory interface (eg., XMLBeanFactory class), then you will need to construct these Configurer classes and call the “postProcessBeanFactory” method by passing the BeanFactory instance as a parameter.

This method is also very helpful if there is a need to centralize the post-processing logic in one place and if this logic involves a lot of custom coding:

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException

As per the Javadocs, this method is invoked after all bean definitions have been loaded, but no beans have been instantiated yet.

Technorati Tags: , , ,

About Shashi Velur

I am Shashi Velur. I live in Austin, TX with my wife and daughter. I consider myself an experienced Enterprise Software Developer/Architect. The views expressed are mine alone and do not necessarily represent the views of my current and any of my previous employers.
This entry was posted in Java and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>