Spring MVC: Sharing context within ear
I do not believe anything has changed from Spring 2.x to 3.x as far as application context hierarchies are concerned.
From what I can tell, the issue with your config is that you are are loading the applicationContext.xml
- the one which is loaded into the sharedContext
, is also being loaded by each webapp, because of the fact that its mentioned in the context-param
contextConfigLocation
.
Since the same file is loaded twice, once in the parent context and once in the web application's root context, there are copies made, and the child context, ie. webapp, uses the ones it created, not the ones that are present in the parent.
Change your config so you don't reload the same beans xml twice, and it should work fine. You can use parentContextKey
and contextConfigLocation
both just don't load the same files.
Update: In addition to the above, you also need to check if the shared jar is visible to the wars (visible as in allowed to share the same instance.). I tried to run the sample from the blog and it did not work for me when I deployed it as a Java EE6 application, and that's because the rules for ear jar visibility inside wars changed from Java EE5 to EE6. When I run the sample in compatibility mode of Glass Fish, everything works as expected.
So check your EAR / WARs to see what servlet spec you are running, and make sure your server is deploying the application accordingly.
If you have to upgrade to Java EE 6, make sure you are following the latest visibility rules http://docs.oracle.com/cd/E19226-01/820-7688/gjjdt/index.html. Check the MANIFEST
files of the wars to ensure they have all ear jars explicitly mentioned in the Class-Path
configuration.
Hope this helps.
Though this question is old one, if someone is wondering why the documented approach is not working in Spring Framework 5.0+. The support for sharing context within ear is currently broken in Spring Framework 5.0+ as of posting of this answer. There is an existing issue open on Spring Framework Issue-20805
I got it solved.
The problem was in class loading as I suspected in comments to @Akshay's answer.
Maven included spring libs inside each war package, so they were loaded multiple times. To fix this, one needs to generate skinny wars.
I assume Akshay's note on his answer to remove the contextConfigLocation
from context-params in web.xml was in key role as well.