Spring MVC and Multiple Spring Contexts
Spring framework is one of the widely used frameworks in Enterprise Java World. Using Spirng MVC requires a proper understanding of ‘Contexts’ of the framework.
Spring has provision for defining multiple contexts in parent/child hierarchy. Spring manages beans that belong to different contexts. The org.springframework.context.ApplicationContext
is consisered as the root context or parent context for all the other contexts.
From the official documentation:
The interface org.springframework.context.ApplicationContext
represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.
Again there is one more context called WebApplicationContext
in Spring MVC, which is a child context. The FrameworkServlet of the spring framework i.e. DispatcherServlet
will have this web context.
Again from the official docs:
In the Web MVC framework, eachDispatcherServlet
has its ownWebApplicationContext
, which inherits all the beans already defined in the rootWebApplicationContext
. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.
The ApplicationContext
related beans are loaded using one of the two following ways.
In web.xml we can have configuration for the org.springframework.web.context.ContextLoaderListener
class, which is a servlet context listener in the following way. This listener will get invoked when a web app gets deployed on the server.
ContextLoaderListener configuration:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/application-context.xml</param-value>
</context-param>
This listener looks for a context-param
named contextConfigLocation
in the web.xml. If it finds one, then the xml meta data file will be scanned for the beans that belong to the root application context. If it doesn’t find any, then the listener will look for a file named applicationContext.xml
in the class path for the configuration meta data.
Similarly the DispatcherServlet
, which is configured in the web.xml will look for an init-param
named contextConfigLocation
in the servlet definition as shown below.
DispatcherServlet configuration:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
If the init-param
is not configured, then a file named dispatcher-servlet.xml
will be searched for in the class path. Tha file name being searched for is made up of the servlet name – in our case ‘dispatcher’ – and a string ‘-servlet.xml’ as suffix.
Note:
The child context i.e. the MVC context will have access to the beans in the parent or root context. But not vice versa.
So, in both the cases i.e. the ContextLoaderListener
or in case of DispatcherServlet
if the parameters are not found in the web.xml and the corresponding files applicationContext.xml
or YOUR SERVLET NAME-servlet.xml
are not found in the class path then a FileNotFoundException
will be thrown and the application won’t start up.
Usually the model classes and other beans that belong to the entire application are configured as beans in the applicationContext.xml. And the classes annotated with @Controller
and the beans that are related to the MVC layer are configured in the DispatcherServlet
configuration file.
Most of the tutorials on web have Spring MVC configuration with a single file included in both the parent context as well as in the child context because of lack of understanding of how Spring works. Due to this all the beans configured in the corresponding xml file will be instantiated twice including any of those beans defined for connection pooling, in which case connection pooling will also be a problem.
If we have only one file with spring beans configuration meta data, then we can configure only that xml file in DispatcherServlet
‘s web context, which is the only mandatory context for an MVC web application, in which case we can exclude the configuration for the servlet context listener ContextLoaderListener
in the web.xml
.
So, whenever one considers using Spring MVC the understanding of these contexts is a must. Hope this article helped you and wish you happy coding.
–Rajasekhar
Java BI Developer
Helical IT Solutions
Best Open Source Business Intelligence Software Helical Insight is Here
Superb and very concise explanation Rajasekhar. I recently faced similar issue and was not able to understand why my context are getting loaded twice.
ANd that too I realized when I tried some other beans which are not allowed to be created twice in application.
My question here is when beans I such way are “Instantiated twice” so there will be twice the number of objects. but then how singleton behavior will be maintained here? Will it be like this that the first time loaded instants will be zombies and not referred any more but second time instances will always be used and referenced.
@Kapil
As you noted correctly, singleton nature of some objects will be lost. That means when the application loads/instantiates, beans will be created twice due to wrong configuration. I think, first time instances are not used and the instances produced later will be consumed.
Hi Thanks for the tutorial.
My question is that if we define contextConfigLocation as init-param, is it becomes part of child context only or it is still in root context.
Good explanation. you are right some spring mvc tutorial has really used context param for initializing dispatcher servlet.
Great .
Can you explain me the way to load XML bean destinations to root context at runtime .Because when I am doing it beans are get added to nested context.
So I was unable to retrieve by type.
Great information. Helped me figure out how to have multiple application contexts. Thank you very much.
Informative post about Spring MVC and Multiple Spring Contextsjava training in chennai
Great explanation sir,
But can you just brief me about , if i don’t give tag for the contextListener in my Web.Xml file, then how it searches for the applicationContext.xml in classpath.
i am not able to understand this classpath search , which is this classpath, Is it the same as we give for jdk in our Environment Variable , or different then this ??
Might sound stupid question, bt thank you in advance !!