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 Test-application-context1.xml
and Test-application-context2.xml
and one web application context Test-dispatcher-servlet.xml
. In all of these contexts I initialized a simple java bean with two fields:
1) In Test-application-context1.xml
:
<bean id="testObject" class="test.TestObject">
<property name="fName" value="FirstName Context1"/>
<property name="lName" value="LastName Context1"/>
</bean>
2) In Test-application-context2.xml
:
<bean id="testObject" class="test.TestObject">
<property name="fName" value="FirstName Context2"/>
<property name="lName" value="LastName Context2"/>
</bean>
3) In Test-dispatcher-servlet.xml
::
<bean id="testObject" class="test.TestObject">
<property name="fName" value="FirstName WebContext"/>
<property name="lName" value="LastName WebContext"/>
</bean>
I also provided a proper configuration in the web.xml
file to initialize all of these contexts when the server starts:
<servlet>
<servlet-name>dispatcherTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Test-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherTest</servlet-name>
<url-pattern>/Test/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Test-application-context1.xml /WEB-INF/Test-application-context2.xml</param-value>
</context-param>
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 ApplicationContextAware
so I tried it like this:
@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());
}
}
In the above example I always get values of testObject
from Test-dispatcher-servlet.xml
so it seems that applicationContext
that is being injected here represents web application context, BUT if I let's say rename testObject
to testObject1
in 'Test-dispatcher-servlet.xml' and run the same code I will get values from Test-application-context2.xml
so here are the questions I have.
1) When we make class implement ApplicationContextAware
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)?
2) What is the proper way to inject multiple application contexts to the class?
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.