title: Spring MVC: Sharing context within ear
body: <p>I have an ear package that contains one jar with common objects and two war webapps that I'd like to use the common jar. I've setup the configuration to use application wide context via ContextLoaderListener and webapp contexts separately for DispatcherServlet.</p>
  
  <p>The setup of my demo app is roughly the following</p>
  
  <ul>
  <li><code>common.jar</code> contains <em>applicationContext.xml</em> and <em>beanRefContext.xml</em>, which are supposed to be application (ear) wide context. The files are like below. <em>shared</em> namespace is where the shared bean is located.</li>
  </ul>
  
  <p><strong>applicationContext</strong></p>
  
  <pre><code>&lt;beans&gt;
      &lt;!-- namespace etc declarations omitted --&gt;
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="study.spring.multicontext.shared" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>beanRefContext.xml</strong></p>
  
  <pre><code>&lt;beans&gt;
      &lt;!-- namespace etc declarations omitted --&gt;
  &lt;bean id="sharedContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt;
      &lt;constructor-arg&gt;
          &lt;list&gt;
              &lt;value&gt;classpath*:applicationContext.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <ul>
  <li><p><code>webapp1</code> and <code>webapp2</code> are Spring MVC applications packaged as separate wars with <strong>web.xml</strong> file like below</p>
  
  <pre><code>&lt;web-app&gt;
  
  &lt;context-param&gt;
    &lt;param-name&gt;parentContextKey&lt;/param-name&gt;
    &lt;param-value&gt;sharedContext&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
    &lt;param-name&gt;locatorFactorySelector&lt;/param-name&gt;
    &lt;param-value&gt;classpath:beanRefContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;classpath*:applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;dos&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  
      &lt;init-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;/WEB-INF/dos-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
  
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dos&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p></li>
  </ul>
  
  <p>and <strong>xx-servlet.xml</strong> like for webapp specific context. <em>web</em> namespace is where the controllers are located.</p>
  
  <pre><code>&lt;beans&gt;
      &lt;!-- namespace etc declarations omitted --&gt;
  
      &lt;context:component-scan base-package="study.spring.multicontext.web"/&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
  
        &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <ul>
  <li><p>The shared bean is @Autowired in normal fashion in Controller classes</p>
  
  <pre><code>@Autowired
  MySharedBean mySharedBean
  </code></pre></li>
  <li><p>ear package contains both wars and jar, and structure is like</p>
  
  <pre><code>ear
   |
   |--common.jar
   |   |--META-INF
   |   |--applicationContext.xml
   |   |--beanRefContext.xml
   |
   |--webapp1.war
   |   |--WEB-INF
   |       |--xx-servlet.xml
   |       |--web.xml
   |
   |--webapp2.war
   |   |--WEB-INF
   |       |--xx-servlet.xml
   |       |--web.xml
  </code></pre></li>
  </ul>
  
  <hr>
  
  <p>The problem is that there will still be two instances of the bean. One for each controller/webapp, since there's only one Controller in each of the wars. I have tried to twiddle with the configuration, but no matter what I do, I either get zero instances or two instances.</p>
  
  <p>I checked the references with Eclipse MAT from a memory dump, and there are actually 4 instances, but I guess the two are for Spring internal use. Anyway, from there it's clearly visible that each controller has it's own instance.</p>
  
  <p>I've read numerous of blog posts, discussion forums, etc where they say that this should be as simple as this. Some suggest JNDI, but as I've understood, this should be possible without it.</p>
  
  <p>And it's not possible to combine the wars and bundle the jar inside. As it might work for this demo app, the real life case I'm working with does not allow this.</p>
  
  <p>Any help on this matter is highly appreciated. Thanks in advance.</p>
  
  <p><a href="http://blog.springsource.org/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/">SpringSource example from 2007</a> for Spring 2.X that does the same but with different configuration. A bit outdated and looking for a Spring 3.X based solution, as dscribed in the bounty description.</p>