Spring MVC(1) - DispatcherServlet initialization

This section mainly talks about the initialization of the DispatcherServlet of Spring MVC. The servlet cannot be started by itself. It is started by the web container (such as tomcat) and initialized by calling the servlet. DispatcherServlet implements and extends javax.Servlet, which is the core of Spring MVC. First paste the basic configuration file, web.xml

<?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">
    <!-- Define the applicationcontext configuration that needs to be initialized -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext-hello.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/hello-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

The ContextLoaderListener configured in web.xml  will initialize  the XmlWebApplicationContext. The function of this class is similar to the  ClassPathXmlApplicationContext  class in Spring. XmlWebApplicationContext is the WebApplicationContext in Spring MVC. It will read the  contextConfigLocation configured in web.xml and be responsible for completing all other spring projects. Context initialization (at least including loading xml of other projects and initializing beans).

Let's continue to analyze the initialization process of DispatcherServlet. Let's take a look at the class structure first. HttpServlet is a Java general servlet implementation provided by the servlet-api.jar package, and FrameworkServlet is an inheritance implementation of spring.



 

 

// DispatcherServlet initialization will call the init() method of the parent class FrameworkServlet
public void init(){
      // Concrete initialization
      initServletBean();
}

// FramworkServlet class implementation
protected final void initServletBean() {
      this.webApplicationContext = initWebApplicationContext();
      initFrameworkServlet();
}

// This method ensures that the WebApplicationContext is created and refreshed to update the configuration file, that is, a comprehensive initialization
protected WebApplicationContext initWebApplicationContext() {
      // if there is a WebApplicationContext
      // Then configure the context, and onRefresh will be called inside the function to load the configuration and initialize
      configureAndRefreshWebApplicationContext (quack);
  
      ....
      // If it does not exist, create a WebApplicationContext,
      // Refresh the context, load the configuration and initialize
      onRefresh (wac);
}

// The onRefresh method is finally implemented in the subclass DispatcherServlet to complete the initialization refresh configuration process
// DispatcherServlet 类
        protected void onRefresh(ApplicationContext context) {
		initStrategies(context);
	}
        // Perform a series of bean initialization, each step will initialize the corresponding bean, responsible for processing its own tasks at runtime
	protected void initStrategies(ApplicationContext context) {
		initMultipartResolver(context);
		initLocaleResolver(context);
		initThemeResolver(context);
		initHandlerMappings(context); // Register HandlerMapping processing class
		initHandlerAdapters(context); // Register HandlerAdapter processing class
		initHandlerExceptionResolvers(context);
		initRequestToViewNameTranslator(context);
		initViewResolvers(context); // register view resolvers
		initFlashMapManager(context);
	}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269145&siteId=291194637