ServletContextInitializer添加 servlet filter listener
ServletContextInitializer添加 servlet filter listener
https://www.cnblogs.com/pomer-huang/p/9639322.html
关于springboot中添加Filter的方法
https://www.jianshu.com/p/3d421fbce734
———————————————————————————————————————————————————————————————
Spring Boot Servlet : ServletContextInitializer
概述
功能介绍
Spring Boot提供的在Servlet 3.0+环境中用于程序化配置ServletContext的接口。该接口ServletContextInitializer主要被RegistrationBean实现用于往ServletContext容器中注册Servlet,Filter或者EventListener。这些ServletContextInitializer的设计目的主要是用于这些实例被Spring IoC容器管理。这些ServletContextInitializer实例不会被SpringServletContainerInitializer检测,因此不会被Servlet容器自动启动。
该接口
ServletContextInitializer和Spring Web的另外一个接口WebApplicationInitializer看起来几乎一模一样。但二者使用目的不同。Spring Web中,WebApplicationInitializer也是针对Servlet 3.0+环境,设计用于程序化配置ServletContext,跟传统的web.xml相对或者配合使用。WebApplicationInitializer实现类会被SpringServletContainerInitializer自动检测和启动。
继承关系

使用
关于ServletContextInitializer的应用可以参考下面的例子。TomcatStarter是Spring Boot Web Servlet应用结合Tomcat使用时的一个ServletContainerInitializer实现。从下面代码不难看出,一组ServletContextInitializer会被设置到ServletContainerInitializer TomcatStarter上,而TomcatStarter在Servlet容器启动过程中调用自己的方法#onStartup,会逐一调用这些ServletContextInitializer的方法#onStartup初始化ServletContext。
package org.springframework.boot.web.embedded.tomcat;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
class TomcatStarter implements ServletContainerInitializer {
private static final Log logger = LogFactory.getLog(TomcatStarter.class);
// 使用者会指定一组 ServletContextInitializer 给 TomcatStarter
private final ServletContextInitializer[] initializers;
private volatile Exception startUpException;
TomcatStarter(ServletContextInitializer[] initializers) {
this.initializers = initializers;
}
// Servlet 容器启动时回会用该方法,该方法会逐一调用每个 ServletContextInitializer 的方法
// #onStartup 会指定 ServletContext 进行初始化。这些 ServletContextInitializer 的目的
// 通常会是 注册 Servlet, Filter 或者 EventListener 。
@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext)
throws ServletException {
try {
for (ServletContextInitializer initializer : this.initializers) {
initializer.onStartup(servletContext);
}
}
catch (Exception ex) {
this.startUpException = ex;
// Prevent Tomcat from logging and re-throwing when we know we can
// deal with it in the main thread, but log for information here.
if (logger.isErrorEnabled()) {
logger.error("Error starting Tomcat context. Exception: "
+ ex.getClass().getName() + ". Message: " + ex.getMessage());
}
}
}
public Exception getStartUpException() {
return this.startUpException;
}
}
源代码
源代码版本 : 2.1.3.RELEASE
package org.springframework.boot.web.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.SpringServletContainerInitializer;
import org.springframework.web.WebApplicationInitializer;
@FunctionalInterface
public interface ServletContextInitializer {
/**
* Configure the given ServletContext with any servlets, filters, listeners
* context-params and attributes necessary for initialization.
* @param servletContext the ServletContext to initialize
* @throws ServletException if any call against the given ServletContext
* throws a ServletException
*/
void onStartup(ServletContext servletContext) throws ServletException;
}
参考文章
——————————————————————————————————————————————————————————————————
springboot 2.1.3.RELEASE添加filter,servlet源码学习
Servlet规范中,通过ServeltContext来注册Filter、Servlet,这里分析Filter,Servlet是相同逻辑
springboot2.0中,我们通过
FilterRegistrationBean将指定得filter来实现ServeltContext注册filter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
FilterRegistrationBean的实例化过程public FilterRegistrationBean(T filter, ServletRegistrationBean... servletRegistrationBeans) { super(servletRegistrationBeans); Assert.notNull(filter, "Filter must not be null"); this.filter = filter;}super的实例化AbstractFilterRegistrationBean(ServletRegistrationBean... servletRegistrationBeans) { Assert.notNull(servletRegistrationBeans, "ServletRegistrationBeans must not be null"); Collections.addAll(this.servletRegistrationBeans, servletRegistrationBeans);} |
可知FilterRegistrationBean得实例化过程就是将Filter保存到servletRegistrationBeans(一个set)中
再分析FilterRegistrationBean类

FilterRegistrationBean的父类是一个ServletContextInitializer,他有一个方法onStartup(ServletContext servletContext)
其结果最终会调用
servletContext.addFilter(this.getOrDeduceName(filter), filter)
现在看看ServletContextInitializer.onStartup的调用地方
|
1
2
3
4
5
6
7
8
9
10
11
12
|
springboot启动new SpringApplication(XXApplication.class).run(XXApplication.class,args)调用refreshContext(context); -> refresh(context); -> ((AbstractApplicationContext) applicationContext).refresh();-> ServletWebServerApplicationContext.onRefresh(); -> createWebServer();WebServer webServer = this.webServer;ServletContext servletContext = getServletContext();if (webServer == null && servletContext == null) { ServletWebServerFactory factory = getWebServerFactory(); this.webServer = factory.getWebServer(getSelfInitializer());} |
再分析getSelfInitializer方法
|
1
2
3
4
|
prepareWebApplicationContext(servletContext); registerApplicationScope(servletContext); WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),servletContext); for (ServletContextInitializer beans :getServletContextInitializerBeans()) { //调用ServletContextInitializer的onStartup方法 |
|
1
|
beans.onStartup(servletContext); }-> getServletContextInitializerBeans() |
|
1
2
|
-> new ServletContextInitializerBeans(getBeanFactory())-> addAdaptableBeans(beanFactory) |
|
1
2
3
4
5
6
7
8
|
addAdaptableBeans(ListableBeanFactory beanFactory) 代码如下:MultipartConfigElement multipartConfig = getMultipartConfig(beanFactory);//将servlet类的bean包装为ServletRegistrationBeanaddAsRegistrationBean(beanFactory, Servlet.class,new ServletRegistrationBeanAdapter(multipartConfig));//将Filter的bean包装为FilterRegistrationBeanaddAsRegistrationBean(beanFactory, Filter.class,new FilterRegistrationBeanAdapter());for (Class<?> listenerType : ServletListenerRegistrationBean.getSupportedTypes()) { addAsRegistrationBean(beanFactory, EventListener.class,(Class<EventListener>) listenerType,new ServletListenerRegistrationBeanAdapter());} |
这里提一下,如果Servlet的bean实例名为dispatcherServlet,且该实例在seen集合中(seen集合为自定义配置的实例,通过ServletContextInitializerBeans.addServletContextInitializerBean入口可以查看源码),则设置默认值为 /
|
1
2
3
4
5
6
7
|
String url = (totalNumberOfSourceBeans != 1) ? "/" + name + "/" : "/";if (name.equals(DISPATCHER_SERVLET_NAME)) { url = "/"; // always map the main dispatcherServlet to "/"}ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<>(source,url); bean.setName(name);bean.setMultipartConfig(this.multipartConfig);return bean; |
总结:springboot启动时,会将所有的 FilterRegistrationBean、ServletRegistrationBean以及被spring管理的Servlet和Filter的实例,通过 ServletContext注册Servlet以及Filter
ServletContextInitializer添加 servlet filter listener的更多相关文章
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
- JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)
JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference
- SpringBoot---注册Servlet,Filter,Listener
1.概述 1.1.当使用 内嵌的Servlet容器(Tomcat.Jetty等)时,将Servlet,Filter,Listener 注册到Servlet容器的方法: 1.1.1.直接注册Bean ...
- servlet filter listener interceptor 知识点
这篇文章主要介绍 servlet filter listener interceptor 之 知识点.博文主要从 概念,生命周期,使命介绍其区别.详情如下: 概念 生命周期 使命 servlet ...
- servlet/filter/listener/interceptor区别与联系
转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...
- 【转】servlet/filter/listener/interceptor区别与联系
原文:https://www.cnblogs.com/doit8791/p/4209442.html 一.概念: 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台 ...
- [转]web.xml中servlet ,filter ,listener ,interceptor的作用与区别
原文链接:https://blog.csdn.net/netdevgirl/article/details/51483273 一.概念: 1.servlet:servlet是一种运行服务器端的java ...
- SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener
简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...
- Spring Boot整合Servlet,Filter,Listener,访问静态资源
目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...
随机推荐
- 浅谈 HTTP协议
1.什么是http协议Hyper Text Transport Portocal(超文本传输协议)HTTP协议是应用层协议浏览器和web服务器通讯时遵守的约定互联网使用最多的协议提供超文本的传输服务通 ...
- docker安装 与 基本配置
1.安装docker #yum remove docker \ docker-common \ container-selinux \ docker-selinux \ docker-engine \ ...
- 敏捷项目管理—Scrum框架总结
Scrum中的角色 Scrum Master——项目负责人.项目经理 保护团队不受外界干扰,是团队的领导和推进者,负责提升 Scrum 团队的工作效率,控制 Scrum 中的“检视和适应”周期过程.与 ...
- vue中select设置默认选中
vue中select设置默认选中 一.总结 一句话总结: 通过v-model来:select上v-model的值为option默认选中的那项的值(value) 二.select设置默认选中实例 < ...
- python 使用夜神模拟器
安装版本为6.2.8.0 1.模拟器安装证书 打开模拟器,点击浏览器 在浏览器里输入:mitm.it 出现如下: 选择安卓进行安装 比如:sks123 2.设置代理 输入密码:sks123 上面刚才设 ...
- 019 spring social
1.原理 2. 3. 4.
- “庚武讲堂”(v.gw66.net) 缘起
转载自: https://v.gw66.net/origin/ 我叫“庚武”,一个从业10余年的程序员,其实我更愿意自称软件工程师或软件设计师.转眼间倏忽十年,从最开始用ASP.net 2.0做网站入 ...
- maven基础依赖外部lib包(依赖钉钉sdk为例)
jar包放置位置 pom.xml指定依赖 1 <dependencies> 2 <!--钉钉工具包,如缺失请到钉钉服务器开发文档下载--> 3 <dependency&g ...
- 由crt和key文件生成keystore文件
该图转自知乎 海棠依旧 1.先生成p12文件,生成的时候需要指定密码 openssl pkcs12 -export -in your_crt.crt -inkey your_key.key -out ...
- ELK 部署相关问题汇总
1.启动es-head问题 因为高版本es-head需要单独启动,所以先要安装npm等工具.安装教程见[1] 启动命令:../elasticsearch-head/node_modules/grunt ...