Spring MVC and Multiple Spring Contexts
Posted By : Shakil Pathan | 30-Nov-2018
Hi Guys,
In this blog, I am going to explain you about multiple contexts of the Spring MVC Framework.
In Enterprise Java Applications Spring framework is one of the widely used frameworks. Using Spring MVC framework requires a proper understanding of 'Contexts' of the framework.
Spring provides functionality for defining multiple contexts in parent and child hierarchy. Spring manages beans those can belong to different contexts. The 'org.springframework.context.ApplicationContext' class is considered as the parent context or root context for all the other contexts.
There is one more context called WebApplicationContext in Spring MVC framework, which is a child context. The FrameworkServlet of the spring framework i.e. DispatcherServlet will have this web context.
The ApplicationContext related beans can be loaded using one of the two following ways:
1. ContextLoaderListener configuration:
In web.xml file we have the configuration for the 'org.springframework.web.context.ContextLoaderListener' class, which is a servlet context listener in the following way. (This listener will get automatically invoked when a web app is 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 above listener looks for a context-param named contextConfigLocation in the web.xml. If it finds any, then the xml meta data file will be scanned for the beans that belong to the parent/root application context. If it doesn’t find any, then this listener will look for a file named applicationContext.xml in the classpath for the configuration meta data.
2. DispatcherServlet configuration:
Similarly the DispatcherServlet, configured in the web.xml, will look for an init-param named as 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>
If the init-param is not configured in the 'web.xml', then a file named dispatcher-servlet.xml will be searched for in the classpath. The 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 IE the MVC context in our case will have access to the beans in the root or parent context. But not vice versa.
So, in both the above cases IE the ContextLoaderListener or in case of DispatcherServlet if the parameters are not found in the web.xml and the corresponding files applicationContext.xml or 'OurServletName'-servlet.xml are not found in the classpath then a FileNotFoundException will be thrown and the application would not start up.
Usually the model classes and other common beans like common services which belong to the entire application are configured as beans in the applicationContext.xml file. And the classes those annotated with @Controller and the beans those are related to the MVC layer are configured in the DispatcherServlet configuration file.
Most of the examples on the web have Spring MVC configuration with in a single file included in both the parent context and the child context because of lack of understanding of how Spring works. Due to this all the beans configured in that 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 a web MVC application, in which case we can exclude the configuration for the servlet context listener ContextLoaderListener in the web.xml.
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 a web MVC application, in which case we can exclude the configuration for the servlet context listener ContextLoaderListener in the web.xml.
Hope this helps.
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shakil Pathan
Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .