The premise to review

We have already written a lot of questions about the implementation principle and core process of SpringMVC, and there is no point in introducing it redundant here. Therefore, we mainly consider the relevant content analysis and explanation for SpringMVC that has not been introduced in the big framework. So let’s plug into one of the three core frameworks of the Spring framework.

The DispatcherServlet genealogy

Inheritance relationships of core classes

The central character

XXAware connected

It is mainly used for BeanPostProcessor to obtain the relevant realization mechanism of XX component functions within the system. After realizing the interface of the implementation class, it is very convenient to obtain some framework information from the Spring framework.

EnvironmentCapable

The mechanism can be implemented through this type of interface to obtain the relevant environment variable objects.

HtpServeltBean

Is a simple implementation interface for HttpServlet abstraction, and a one-step implementation extension for related functionality

FrameworkServlet implementation class

A basic parent of the Spring Web framework that creates a relationship between the parent container and the self container before the dispatcherServlet is created.

DispatcherServlet implementation class

It is mainly used to coordinate the overall operation flow and execution flow of SpringMVC and initialize various component mechanisms, such as: HandlerMapping component, HandlerAdapter component, and HandlerExecuteChain.

Borrow a web map (too lazy to draw it yourself)

The container creation process

There are two main types of containers in the Spring framework and MVC framework domain: we define them as business containers and Web containers. The container will advance to establish a business container (also known as the parent container mechanism), and then create a Web container (child container). When initializing the Web container, the parent container will be bound to the child container as its parent container.

Initialization entry for the parent container

It mainly depends on the contextInitialized method of the ContextLoaderListener in our system. When the ServletContext class is loaded, the listening contextInitailized method will be called by the Servlet container.

The process of creating a parent container

After contextIntialized method is invoked, will create createWebApplicationContext method, called internal determineContextClass method, initialization to judge the type of container, The default is the XmlWebApplicationContext object class. InstantiateClass method is mainly used for reflection to generate corresponding container objects.

In addition to SprringBoot and other types of containers for more likely choose ConfigureAndRefreshWebApplicationContext or AnnotationConfigApplicationContext class, The former is more of a refresher container implementation and use, while the latter is built primarily in JavaConfig.

Finally, the produced parent container objects are set and injected into the global context area of the associated ServletContext container.

Initialization entry for the child container

The HttpServletBean core class overrides the init method of the httpServlet class, which is the entry point for creating a Web container.

  1. A call to the init method of the HttpServletBean
  2. The associated nitFrameworkServlet method is called
  3. The initServletBean method inside the FrameworkServlet is called.

Initialization process for child containers

  1. The container of servletContext gets the container object created in ContextServletListener
  2. If this WebApplicationContext is not empty, set up the configuration related parent container and refresh the container.
  3. After creating the corresponding Web container, use the above container as the parent of the container, rootContext as the parent, and bind the same Settings to the corresponding ServletContext.