Detailed explanation of the SpringMVC startup process (li)

Through in-depth research on the SpringMVC startup process, it is expected to master the Java Web container startup process; master the SpringMVC startup process; understand how the configuration file of SpringMVC is configured and why it is configured in this way; master how SpringMVC works; master the design and enhanced reading of Spring source code Source code skills.

content

1. Web container initialization process

2. Web.xml configuration in SpringMVC

3. Know ServletContextListener

4. Get to know ContextLoaderListener

5.DispatcherServlet初始化(HttpServletBean • FrameworkServlet • DispatcherServlet

6. Relationship between ContextLoaderListener and DispatcherServlet

7. Design of DispatcherServlet

8. How DispatcherServlet works

 

1. Web container initialization process

The above figure shows the process of web container initialization, and its official documentation gives this description:

  When a web application is deployed into a container, the following steps must be performed, in this order, before the web application begins processing client requests.

  1. Instantiate an instance of each event listener identified by a <listener> element in the deployment descriptor.
  2. For instantiated listener instances that implement ServletContextListener, call the contextInitialized() method.
  3. Instantiate an instance of each filter identified by a <filter> element in the deployment descriptor and call each filter instance's init() method.
  4. Instantiate an instance of each servlet identified by a <servlet> element that includes a <load-on-startup> element in the order defined by the load-on-startup element values, and call each servlet instance's init() method.

Second, the configuration of web.xml in SpringMVC

The above figure is the configuration in the intercepted web.xml, the spring container loader is defined in the <listener> tag; the spring front-end controller is defined in the <servlet> tag.

The above figure is the definition of the interface ServletContextListener in the source code. You can see that it is indicated in its comments: before and after the servlet and Filter are initialized and destroyed, they will send corresponding notifications to the listeners that implement the servletContextListener interface.

上面是类ContextLoadListener的定义,它实现了上面的servletContextListener。这里用到了代理模式,简单的代理了ContextLoader类。ContextLoadListener类用来创建Spring application context,并且将application context注册到servletContext里面去。

结合上面的WEB容器启动的过程,以及接口ServletContextListener和类ContextLoadListener。我们知道:

  在 Servlet API中有一个ServletContextListener接口,它能够监听ServletContext对象的生命周期,实际上就是监听Web应用的生命周期。当Servlet容器启动或终止Web应用时,会触发ServletContextEvent事件,该事件由ServletContextListener来处理。在ServletContextListener接口中定义了处理ServletContextEvent 事件的两个方法contextInitialized()和contextDestroyed()。

  ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置了这个监听器,启动容器时,就会默认执行它实现的方法。由于在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

上面是initWebApplicationContext的过程,方法名称即是其含义。方法中首先创建了WebApplicationContext,配置并且刷新实例化整个SpringApplicationContext中的Bean。因此,如果我们的Bean配置出错的话,在容器启动的时候,会抛异常出来的。

  综上,ContextLoaderListener类起着至关重要的作用。它读取web.xml中配置的context-param中的配置文件,提前在web容器初始化前准备业务对应的Application context;将创建好的Application context放置于ServletContext中,为springMVC部分的初始化做好准备。

三、DispatchServlet初始化

  在SpringMVC架构中,DispatchServlet负责请求分发,起到控制器的作用。下面详细来解释说明:

DispatchServlet名如其义,它的本质上是一个Servlet。从上面图可以看到,下层的子类不断的对HttpServlet父类进行方法扩展。

上图是抽象类HttpServletBean的实现,我们知道HttpServlet有两大核心方法:init()和service()方法。HttpServletBean重写了init()方法,在这部分,我们可以看到其实现思路:公共的部分统一来实现,变化的部分统一来抽象,交给其子类来实现,故用了abstract class来修饰类名。此外,HttpServletBean提供了一个HttpServlet的抽象实现,使的Servlet不再关心init-param部分的赋值,让servlet更关注于自身Bean初始化的实现。

上图是FrameworkServlet的官方定义, 它提供了整合web javabean和spring application context的整合方案。那么它是如何实现的呢?在源码中我们可以看到通过执行initWebApplicationContext()方法和initFrameworkServlet()方法实现。

DispatchServlet是HTTP请求的中央调度处理器,它将web请求转发给controller层处理,它提供了敏捷的映射和异常处理机制。DispatchServlet转发请求的核心代码在doService()方法中实现,详细代码参照图上。

上图是DispatchServlet类和ContextLoaderListener类的关系图。首先,用ContextLoaderListener初始化上下文,接着使用DispatchServlet来初始化WebMVC的上下文。

上图是DispatchServlet的工作流程图,作为HTTP请求的中央控制器,它在SpringMVC中起着分发请求的作用。下面总结了DispatchServlet设计的一些特点总结。

四、请求流程

Guess you like

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