title: How to make a class aware of multiple application contexts in Spring MVC?
body: <p>I'm trying to understand the concepts of application context in Spring MVC. To examine it I have created an application with two application contexts <code>Test-application-context1.xml</code> and <code>Test-application-context2.xml</code> and one web application context <code>Test-dispatcher-servlet.xml</code>. In all of these contexts I initialized a simple java bean with two fields:</p>
  
  <p>1) In <code>Test-application-context1.xml</code>:</p>
  
  <pre><code>&lt;bean id="testObject" class="test.TestObject"&gt;
      &lt;property name="fName" value="FirstName Context1"/&gt;
      &lt;property name="lName" value="LastName Context1"/&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>2) In <code>Test-application-context2.xml</code>:</p>
  
  <pre><code>&lt;bean id="testObject" class="test.TestObject"&gt;
      &lt;property name="fName" value="FirstName Context2"/&gt;
      &lt;property name="lName" value="LastName Context2"/&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>3) In <code>Test-dispatcher-servlet.xml</code>::</p>
  
  <pre><code>&lt;bean id="testObject" class="test.TestObject"&gt;
      &lt;property name="fName" value="FirstName WebContext"/&gt;
      &lt;property name="lName" value="LastName WebContext"/&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>I also provided a proper configuration in the <code>web.xml</code> file to initialize all of these contexts when the server starts:</p>
  
  <pre><code> &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcherTest&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/Test-dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;3&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dispatcherTest&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Test/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/Test-application-context1.xml /WEB-INF/Test-application-context2.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  </code></pre>
  
  <p>Now I want to inject these application/web application contexts in one of my controller classes. I'm not sure how to do this for multiple contexts properly. I know when I have one context I can make my class implement <code>ApplicationContextAware</code> so I tried it like this: </p>
  
  <pre><code>@Controller
  public class TestController implements ApplicationContextAware{
  
  private ApplicationContext applicationContext;
  
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      this.applicationContext = applicationContext;
  }
  
  @RequestMapping(value = "/Test")
  public void Test(HttpServletRequest request) {
      TestObject testObject = applicationContext.getBean("testObject", TestObject.class);
      System.out.println("fName="+testObject.getfName()+"; lName="+testObject.getlName());
      }
  }
  </code></pre>
  
  <p>In the above example I always get values of <code>testObject</code> from <code>Test-dispatcher-servlet.xml</code> so it seems that <code>applicationContext</code> that is being injected here represents web application context, BUT if I let's say rename  <code>testObject</code> to <code>testObject1</code> in 'Test-dispatcher-servlet.xml'  and run the same code I will get values from <code>Test-application-context2.xml</code> so here are the questions I have.</p>
  
  <p>1) When we make class implement <code>ApplicationContextAware</code> having multiple contexts which context will be injected to that class? Is it one context that is being injected or does spring somehow combines all of contexts and inject them as one applicationContext object (that would explain why  do I get values from a different context when I change the name of the bean in one of the contexts)? </p>
  
  <p>2) What is the proper way to inject multiple application contexts to the class?</p>
  
  <p>I know the above example is not a typical scenario and probably a bad design pattern but I'm just trying to understand how the whole thing works.</p>