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:
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.