The main program of Spring MVC is DispatcherServlet. Most Spring MVC webapp’s web.xml is as follows.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Above example shows that every url is handled by DispatcherServlet. DispatcherServlet itself is JEE servlet. Without understanding inner working, we can not correlate several Spring MVC components (ex. controller, view, interceptor)
The following is inner working of the program (based on Spring 4.3.11)
Initialization
- Main Spring context is initialized by ContextLoaderListener. Usually “contextConfigLocation” param indicate main bean config file. (which is inside <context-param>)
- DispatcherServlet context is initialized by “contextConfigLocation” param (which is child of <init-param>). This context is child of main Spring context
- Therefore, beans in root-context.xml can not reference beans in servlet-context.xml.
Main sub component
- Main logic is implemented at DispatcherServlet.doDispatch()
- HandlerExecutionChain contains handler and interceptors. Handler is usually controller class (where @Controller is defined)
- HandlerAdapter is the execution engine of the handler
- HandlerAdapter itself can render view (return is null) or delegate rendering to View (return is ModelAndView)
- ModelAndView is the container for the result data and view
Main processing sequence
- HandlerExecutionChain is created
- Based on HandlerExecutionChain, a specific type of HandlerAdapter is created
- Based on HandlerExecutionChain, each interceptor’s preHandle() is executed. (org.springframework.web.servlet.HandlerInterceptor)
- Generated handler adapter is executed. This is the point where controller is executed. And the result type is ModelAndView
- Based on HandlerExecutionChain, each interceptor’s postHandle() is executed
- If ModelAndView is not null, matching view’s render() is executed. This is the point where response is shown. Here, a specific view is resolved by ViewResolver (More on ViewResolver)
One thought on “The inner working of DispatcherServlet”