Spring PropertyPlaceholderConfigurer within web and application context

I ran into an issue where I was attempting to load properties for a Spring 3.0 web application in both the application context configuration and the web application configuration.The configuration was initially defined as follows:

applicationContext.xml

Both the application context and the web context referenced the same ‘payment.proxy.X’ placeholders, however, only the placeholders in the application context resolved (ENV_CODE is resolved from the System properties). I found the following quote from Jürgen Höller, one of the founder of Spring, on the spring-dev list from a long time back:

PropertyPlaceholderConfigurer is an implementation of the
BeanFactoryPostProcessor interface: This interface and its sibling
BeanPostProcessor just apply to the BeanFactory that defines them, that is,
to the application context that defines them.

If you combine multiple config files into a single contextConfigLocation, a
PropertyPlaceholderConfigurer defined in any of the files will apply to all
of the files, because they are loaded into a single application context.

However, a DispatcherServlet has its own application context, just using the
root web application context as parent. Therefore, it needs to define its
own BeanFactoryPostProcessors and/or BeanPostProcessors, in this case its
own PropertyPlaceholderConfigurer.

I believe that this is the better general solution: Each application context
might want to resolve its placeholders against its own properties files,
where applying PropertyPlaceholderConfigurer from parent contexts would
interfere.

I then defined the same PropertyPlaceholderConfigurer in both the application context and the web context and the issue resolved.

Advertisements
This entry was posted in Java, Spring and tagged , , , , , . Bookmark the permalink.

2 Responses to Spring PropertyPlaceholderConfigurer within web and application context

  1. rkb says:

    So you saying that this will not work for following case

    You load the PropertyPlaceholderConfigurer context xml at the parentRootContext.

    These properties are not available to the application context that are not part of the parentRootContext, in case of multiple context roots.

    • asoftwareguy says:

      So if I understand correctly, you are describing a scenario as follows:

      Application Context Relation

      If this is the case then yes, the propertyPlaceholder defined in subContext1.xml will not be available in subContext2.xml. If it were instead defined in applicationContext.xml itself, then it should be available in both of the subContext XMLs. I haven’t used Spring in a while (working on a Grails project at the moment) so I am not sure if it has changed, but this was the behavior I observed when I was using it. The applicationContext and the webContext definitely are in separate contexts and will require separately defined propertyPlaceholders.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s