The startup process of Spring MVC

I. Overview

The following is a basic configuration of web.xml using springMVC. There are two places to pay attention to here, one is ContextLoadListener and the other is DispatcherServlet. It is through these two configurations that the web container is associated with Spring. These two configurations are associated with the ServletContext of the web container, which provides a host for Spring's Ioc container. After the Ioc container system is established, DispatcherServlet is established as a forwarder for Spring MVC to process web requests, thereby completing the preparation for responding to Http requests. .

The Spring MVC startup process is roughly divided into two processes based on these two configurations:

1. The ContextLoaderListener is initialized, the IoC container is instantiated, and the container instance is registered in the ServletContext.

2. The DispatcherServlet is initialized, establishes its own context, and is also registered in the ServletContext.

copy code
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/controllers.xml,/WEB-INF/service.xml</param-value>
</context-param>
<servlet>
  <servlet-name>dispatch</servlet>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>dispatch</servlet-name>
  <servlet-pattern>*.*</servlet-pattern>
</servlet-mapping>
</web-app>
copy code

Second, the startup of the Spring IOC container

ContextLoaderListener实现ServletContextListener,这个接口里面的函数会结合web容器的生命周期被调用。因为ServletContextListener是ServletContext的监听者,如果ServletContext发生变化,会触发相应的事件,而监听者一直对这些事件进行监听,如果接受到了监听的事件,就会作出预先设计好的动作。例如在服务器启动,ServletContext被创建的时候,ServletContextListener的contextInitialized()方法被调用,从而拉开了初始化Spring IOC容器的大幕。

 

首先从Servlet的启动事件中得到ServletContext,然后读取web.xml中的各个相关的属性值,接着ContextLoader会实例化WebApplicationContext,并完成载入和初始化的过程,这个被初始化的第一个上下文作为根上下文而存在,这个根上下文载入后,被绑定到web应用程序的ServletContext上,这样,IOC容器中的类就可以在任何地方访问到了。

具体的Ioc容器的载入过程在refresh()中实现,这个方法主要干了以下几件事情:

(1)Resource文件的定位,即找到bean的配置文件
(2)通过特定的reader解析该bean配置文件,抽象成beanDefinition类
(3)将beanDefinition向容器注册,写入到一个大的HashMap中,供实例化请求的时候使用

三、DispatchServlet的启动

DispatchServlet本质上是一个Servlet,web容器启动的时候,servlet也会初始化,其init方法被调用,开启初始化之旅。

DispatchServlet will establish its own context to hold Spring MVC special bean objects. When establishing the Ioc container it owns, it will get the root context from ServletContext as the parent context of the DispatchServlet context. With this root context, initialize the context held by yourself, and finally save the context held by yourself to the ServletContext for later retrieval and use.

The initialization of its own context is completed in initWebApplicationContext, and there is also a refresh process, which is similar to the initialization of ordinary Ioc containers.

Some other MVC features are implemented in initStrategies() during initialization, including LocalResolver supporting internationalization, HandlerMappings supporting Request mapping, and ViewResolver generated by views, etc.

Third, the distribution of DispatcherServlet handles Http requests

After the initialization is completed, all the HandlerMappings defined in the context have been loaded. These loaded HandlerMappings are placed in the List to store the mapping data of Http requests and Controllers.

DispatcherServlet is a subclass of HttpServlet, and it also responds to HTTP requests through doService(). The doDispatch() method is used in doService().

(1) First through url matching, find Handler through getHandler, and the configured Controller is encapsulated in Hander

(2) Execute Handler to get the returned ModelAndView result

(3) Finally, the ModelAndView object is handed over to the render() method of the view object for presentation.

 

References:

"Spring Technology Insider: In-depth Analysis of Spring Architecture and Design Principles"

http://spring.group.iteye.com/group/wiki/1226

Guess you like

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