从头看看Tomcat启动Spring容器的原理
通过带注解Spring Boot可以启动一个web容器,并初始化bean容器。那么Tomcat启动并初始化spring容器的原理是怎样的?
Tomcat启动web程序时会创建一对父子容器(图1):
有几种方式:
- XML配置Spring和Servlet容器
- 通过注解初始化
- Servlet提供SPI的调用方式来启动
XML配置Spring和Servlet容器
web.xml主要通过一下配置初始化父子容器(父子容器是职责单一的设计)
<web-app>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!-- 初始化父容器-->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化子容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Listener主要作用就是初始化spring的bean容器,servlet做request的转发
<listener>==> @WebListerner
<Servlet> ==> @WebServlet
<Filter> ==> @WebFilter Servlet提供SPI的调用方式来启动
打开service文件有一行如下:
org.springframework.web.SpringServletContainerInitializer 就是给Tomcat提供初始化容器用的服务接口,Tomcat启动的时候就会通过ServiceLoader把这个类加载进来,然后调用该类的onStartUp方法来初始化
@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
注解中的WebApplicationInitializer.clas就是引入的容器初始化类了。
看一下他的子类:
public abstract class SpringBootServletInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
WebApplicationContext rootApplicationContext = this.createRootApplicationContext(servletContext);
if (rootApplicationContext != null) {
servletContext.addListener(new SpringBootServletInitializer.SpringBootContextLoaderListener(rootApplicationContext, servletContext));
} else {
this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
}
}
}
其中的onStartup方法中有addListener,这个添加的Listener正是对应的XML中:<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
其中的SpringBootContextLoaderListener是该类的内部类,继承自ContextLoaderListener
其中的this.createRootApplicationContext(servletContext);便是创建图1中的父容器
从头看看Tomcat启动Spring容器的原理的更多相关文章
- spring容器IOC原理解析
原理简单介绍: Spring容器的原理,其实就是通过解析xml文件,或取到用户配置的bean,然后通过反射将这些bean挨个放到集合中,然后对外提供一个getBean()方法,以便我们获得这些bean ...
- 8 -- 深入使用Spring -- 7...1 启动Spring 容器
8.7.1 启动Spring容器 对于使用Spring的Web应用,无须手动创建Spring容器,而是通过配置文件声明式地创建Spring容器.因此,在Web应用中创建Spring容器有如下两种方式: ...
- 基于反射启动Spring容器
基于反射启动Spring容器 package com.maple.test; import org.springframework.context.ApplicationContext; import ...
- tomcat 启动Spring boot 项目
SpringBoot 项目如何在tomcat容器中运行 1.相关连接: https://blog.csdn.net/u010598360/article/details/78789197/ 2.修改打 ...
- springMVC项目部署 服务器启动spring容器报错bean未从类加载器中找到
bean未从类加载器中找到 警告: Exception encountered during context initialization - cancelling refresh attempt: ...
- spring容器启动
1 主要类 ContextLoaderListener:注册在web.xml中,web应用启动时,会创建它,并回调它的initWebApplicationContext()方法,从而创建并启动spri ...
- spring容器启动的三种方式
一.在Web项目中,启动Spring容器的方式有三种,ContextLoaderListener.ContextLoadServlet.ContextLoaderPlugin. 1.1.监听器方式: ...
- ServletContext与Web应用以及Spring容器启动
一.ServletContext对象获取Demo Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象. 可以把ServletContext看作一个 ...
- spring容器IOC创建对象<二>
问题?spring是如何创建对象的?什么时候创建对象?有几种创建方式?测试对象是单例的还是多例的 ?对象的初始化和销毁? 下面的四大模块IOC的内容了!需要深刻理解 SpringIOC定义:把对象的创 ...
随机推荐
- C#算法设计查找篇之03-插值查找
插值查找(Interpolation Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/701 访问. 插值 ...
- do...while循环语句(水仙花)
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h&g ...
- HTML基础-05
字体 文本颜色:color:red;字体分类: 衬线字体serif --字体宽度各异,有衬线 --Times.Georgia.宋体 无衬线字体sans-serif --字体宽度各异,无衬线 --Hel ...
- 实验室外的攻防战 UOJ#180 [树状数组]
实验室外的攻防战 UOJ#180 [树状数组] 题目 时针指向午夜十二点,约定的日子--2月28日终于到来了.随着一声枪响,伏特跳蚤国王率领着他的跳蚤大军们包围了 \(picks\) 博士所在的实验室 ...
- 性能测试必备知识(11)- 怎么理解内存中的Buffer和Cache?
做性能测试的必备知识系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1806772.html 缓存 从 free 命令可以看到,缓存其实就 ...
- 【SP2916】Can you answer these queries V - 线段树
题面 You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A qu ...
- Java继承后访问成员的特点
继承后的特点--成员变量 对象访问成员变量时,会先在子类中查找有没有定义对应的变量,若子类中存在就会就近使用子类中的变量,若子类中没有定义就会沿着继承关系往上找有没有定义相应的变量,若父类中也没有则编 ...
- MIT 6.828 | JOS | 关于虚拟空间和物理空间的总结
Question: 做lab过程中越来越迷糊,为什么一会儿虚拟地址是4G 物理地址也是4G ,那这有什么作用呢? 解决途径: 停下来,根据当前lab的进展,再回头看上学期操作系统的ppt & ...
- openstack vnc 报1006的错误
1.问题现象 创建完虚拟机以后,通过nova get-vnc-console命令,获取虚机的vnc连接地址,在浏览器中打开该连接,报1006错误 2.vnc的原理图 3.定位分析 1)分别在控制节点和 ...
- SQL联结笔记(内联结,自联结,自然联结,外联结区别以及应用)
SQL中有三种联结,分别是:内联结,自然联结,外联结. 联结是针对不同表联合起来的一种方式.应用的对象是:表(table) 为了方便验证练习理解,首先展示所要用到的表的内容: 1.Customers表 ...

