SpringBoot消失的Web.xml
Filter
过滤器作为web.xml中重要的一部分,有着相当高的出场率,SpringBoot会默认注册几个Filter
ApplicationContextHeaderFilter
CharacterEncodingFilter
如果添加了Security依赖的话会加入SpringSecurityFilterChain
如果加入Actuator依赖的话就会加入WebRequestTraceFilter
实现自己的Filter
JavaConfig注册Bean
我们如果自己要实现自己的Filter的话,需要实现Filter并实现其中的方法
同时要利用JavaConfig的方法来配置,一般情况下需要编写@Bean注解的返回值为FilterRegistrationBean的方法来实现JavaBean的注册
具体实现如下

需要注意的是此方法需要在被@Configuration注解的配置类中
@WebFilter+@ServletComponentScan
如果觉得Java代码的方式比较繁琐的话可以采用注解方式注册Filter,具体实现方式是在Filter实现类加入@WebFilter注解
例如

然后在SpringBootApplication类上添加@ServletComponentScan
Filter的注册原理
我们采用JavaConfig的形式实现了Filter的注册,通过向上追溯得知FilterRegistrationBean的层级结构如下
ServletContextInitializer
RegistrationBean
AbstractFilterRegistrationBean
FilterRegistrationBean
经查阅SpringBoot文档发现针对ServletContextInitializer的描述如下
Interface used to configure a Servlet 3.0+
contextprogrammatically.
UnlikeWebApplicationInitializer,
classes that implement this interface (and do not implementWebApplicationInitializer)
will not be detected bySpringServletContainerInitializerand
hence will not be automatically bootstrapped by the Servlet container.This interface is primarily designed to allow
ServletContextInitializers
to be managed by Spring and not the Servlet container.For configuration examples see
WebApplicationInitializer.
既然是由SpringBoot进行管理而不是由Servlet容器管理,那么基本可以确定是由SpringBoot进行管理
在org.springframework.boot.context.embedded.tomcat包中我们找到了答案
TomcatEmbeddedServletContainerFactory的一直向上继承了AbstractConfigurableEmbeddedServletContainer
并且维护了一个私有的List<ServletContextInitializer>变量,我们不难猜出,正是因为FilterRegistrationBean继承了ServletContextInitializer而实现了Filter的注册
为了进一步验证我们的猜测,在注册Filter的JavaConfig代码中打了断点跟踪一下

可以看到在启动过程中会获取类型为ServletContextInitializer的Bean
继续向下看在SpringBoot内嵌的Tomcat中的TomcatStarter类中也同样实现了ServletContextInitializer

并且在实现方法中执行了AbstractFilterRegistrationBean实现的onStartup方法

至此Filter注册成功
Servlet和Listener
Servlet与Listener的支持与Filter大同小异,同样也是支持两种方法进行注册
JavaConfig的话不同的是Servlet需要的是ServletRegistrationBean,而Listener需要的是ServletListenerRegistrationBean
注解的话则分别是通过@WebServlet、@WebListener进行注解
至于注册管理过程则基本与Filter相同
SpringBoot消失的Web.xml的更多相关文章
- SpringBoot源码篇:深度分析SpringBoot如何省去web.xml
一.前言 从本博文开始,正式开启Spring及SpringBoot源码分析之旅.这可能是一个漫长的过程,因为本人之前阅读源码都是很片面的,对Spring源码没有一个系统的认识.从本文开始我会持续更新, ...
- 在springBoot中配置web.xml中配置的servlet
第一种 web.xml (截取的需要转换的) 当拦截到 /socke t时执行该servlet <servlet> <servlet-name>websocket</se ...
- 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中
把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...
- SpringBoot源码篇:Spring5内置tomcat实现code-based的web.xml实现
一.简介 上篇文章讲了SpingBoot诞生的历史背景和技术演进背景,并通过源码说明了SpringBoot是如何实现零配置的包括如何省去web.xml配置的原理.本文接上一篇文章,通过demo演示Sp ...
- 精尽Spring MVC源码分析 - 寻找遗失的 web.xml
该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...
- web.xml常用标签整理(不定期更新)
<?xml version="1.0" encoding="UTF-8"?><!-- 标明使用的XML版本和文档编码,此项必须位于第一行,之前 ...
- Spring-Boot快速搭建web项目详细总结
最近在学习Spring Boot 相关的技术,刚接触就有种相见恨晚的感觉,因为用spring boot进行项目的搭建是在太方便了,我们往往只需要很简单的几步,便可完成一个spring MVC项目的搭建 ...
- 使用idea+springboot+Mybatis搭建web项目
使用idea+springboot+Mybatis搭建web项目 springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便. 1.创建项目project, ...
- spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)
spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...
随机推荐
- JAVASCRIPT闭包以及原型链
方法内部还有个方法,实例化父方法后,再次调用父方法,可以运行父方法内部的子方法,这样的程序就叫做闭包 DEMO如下: //function outerFn() { // var outerVar = ...
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- poj1142Smith Numbers质因子分解
题意:一个数不是质数,其质因子的每位加起来等于该数的每位加起来. /* 题意:一个数的所有质因子的每位相加起来等于该数的每位相加起来且该数不能是质数,那么就是史密斯数 tip:对于分解质因子,只需要判 ...
- 五、Pandas玩转数据
Series的简单运算 import numpy as np import pandas as pd s1=pd.Series([1,2,3],index=['A','B','C']) print(s ...
- CPP-网络/通信:COM
))//打开串口 { ) { CloseCom();//关闭串口 break; } //添加处理代码. } //最后关闭串口 CloseCom();//关闭串口
- Java产生GUID
/** * 产生GUID */public static final String generateGUID(){ UUID uuid = UUID.randomUUID(); return uuid ...
- java集合测试类等
package demo.mytest; import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;import ja ...
- 用Python写一个小爬虫吧!
学习了一段时间的web前端,感觉有点看不清前进的方向,于是就写了一个小爬虫,爬了51job上前端相关的岗位,看看招聘方对技术方面的需求,再有针对性的学习. 我在此之前接触过Python,也写过一些小脚 ...
- virtualenvwrapper.sh报错: There was a problem running the initialization hooks.解决
由于在ubuntu环境下,将python做与python3.6做了软链接(ln -s python python3.6),并且pip也被我做了软链接,所以导致用pip安装virtualenvwrapp ...
- linux设备驱动程序 - 待解决问题记录
1.每个模式都有自己的内存映射,也即自己的地址空间?(P26) http://www.cnblogs.com/wuchanming/p/4360277.html (不知道是不是,没时间看)