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.
org.springframework.context.ApplicationContextrepresents 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, each
DispatcherServlethas its own
WebApplicationContext, which inherits all the beans already defined in the root
WebApplicationContext. 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.
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.
<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
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.
DispatcherServlet, which is configured in the web.xml will look for an
contextConfigLocation in the servlet definition as shown below.
<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>
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.
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
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
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.
Java BI Developer
Helical IT Solutions
Best Open Source Business Intelligence Software Helical Insight is Here