通过tomcat容器启动spring容器的启动过程

参考网站:https://www.cnblogs.com/RunForLove/p/5688731.html

通过对spring源码的解读,跟踪spring容器的启动过程,掌握SpringMVC是如何工作的;掌握Spring源码的设计和增强阅读源码的技巧。为可持续性的使用spring框架高效集成开发,解决系统一些疑难杂症提供强有力的支撑。

一、需要掌握的理论业务点:

1、Web容器初始化过程

2、Spring MVC 在web.xml中的配置

3、理解ServletContextListener

4、掌握理解ContextLoadListener

5、DispatcherServlet初始化(HttpServletBean • FrameworkServlet • DispatcherServlet)

6、ContextLoadListener与DispatcherServlet的关系

7、DispatcherServlet的设计

8、DispatcherServlet工作原理

二、分析各个理论节点的知识

1、要想了解spring容器在web容器中启动的过程,首先我们需要知道web容器在tomcat中的生命周期,方可理解spring容器是如何在web容器中启动、运行、以及销毁、的生命周期

上图展示了web容器在启动过程中主要业务走向,其官方给出的解释是:

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.

翻译是:

web应用程序部署到容器中时,必须按照以下顺序在web应用程序开始处理客户机请求之前执行以下步骤。

实例化由部署描述符中的元素标识的每个事件侦听器的实例。

对于实现ServletContextListener的实例化侦听器实例,调用contextInitialized()方法。

实例化由部署描述符中的元素标识的每个过滤器的实例,并调用每个过滤器实例的init()方法。

实例化由元素标识的每个servlet的实例,该元素按加载启动元素值定义的顺序包含元素,并调用每个servlet实例的init()方法。

大致的意思是,tomcat在启动web容器的时候会启动一个叫ServletContextListener的监听器,每当在web容器中有ServletContextListener这个接口被实例化的时候,web容器会通知ServletContextListener被实例的对象去执行其contextInitialized()的方法进行相应的业务处理,而spring框架在设计的过程中ContextLoadListener这个类实现了ServletContextListener这个接口,因此每当有ContextLoadListener这个类被实例化的时候,web容器会通知他执行contextInitialized()这个方法

2、spring MVC在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">
	<display-name>app</display-name>

	<!-- 加载Spring配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-starter.xml</param-value>
	</context-param>

	<!-- Spring监听器 -->
	<listener>
		<description>Spring监听器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

 在web.xm配置中由<context-param>将spring的配置文件引入加载到web容器中,

<listener>配置项是在web容器中启用监听,其所配置的class是需要启用的监听器,因此spring容器的启用是同过org.springframework.web.context.ContextLoaderListener进行spring容器的启动,而ContextLoaderListener监听实现的是ServlectContextListener接口,因此在其实例化的过程中,contextInitialized()会被调用,从而进行spring容器的启动与创建的过程中

ContextLoaderListener中的contextInitialized()进行了spring容器的启动配置并且刷新实例化整个SpringApplicationContext中的Bean。因此,如果我们的Bean配置出错的话,在容器启动的时候,会抛异常出来的。

@Override
public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
}

通过SerlvetContextListener这一特性还可以做其它的运用,如缓存数据在服务启动过程中的初始化

https://blog.csdn.net/qq_31854907/article/details/86304955

 

猜你喜欢

转载自blog.csdn.net/qq_31854907/article/details/86300901