SpringBoot初始教程之Servlet、Filter、Listener配置(七)
来源:互联网 发布:杭州市行知小学 编辑:程序博客网 时间:2024/04/30 14:54
SpringBoot初始教程之Servlet、Filter、Listener配置(七)
1.介绍
通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的
因为有可能打包之后是一个jar包的形式,这种情况下如何解决?SpringBoot 提供了两种方案进行解决
2.快速开始
2.1 方案一
方案一采用原生Servlet3.0的注解进行配置、@WebServlet
、@WebListener
、@WebFilter
是Servlet3.0 api中提供的注解
通过注解可以完全代替web.xml中的配置,下面是一个简单的配置
IndexServlet
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
IndexListener
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
IndexFilter
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
上面配置完了,需要配置一个核心的注解@ServletComponentScan
,具体配置项如下,可以配置扫描的路径
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
把注解加到入口处启动即可
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2.2 方案二
方案二是采用自己SpringBoot 配置bean的方式进行配置的,SpringBoot提供了三种BeanFilterRegistrationBean
、ServletRegistrationBean
、ServletListenerRegistrationBean
分别对应配置原生的Filter、Servlet、Listener,下面提供的三个配置和方案一采用的方式能够达到统一的效果
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
3.总结
两种方案在使用上有差别,但是在内部SpringBoot的实现上是无差别的,即使使用的是Servlet3.0注解,也是通过扫描注解
转换成这三种bean的FilterRegistrationBean
、ServletRegistrationBean
、ServletListenerRegistrationBean
4.扩展
大家在使用的时候有没有发觉,其实SpringBoot在使用SpringMvc的时候不需要配置DispatcherServlet的,因为已经自动配置了,
但是如果想要加一些初始配置参数如何解决,方案如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
可以通过注入DispatcherServlet 然后用ServletRegistrationBean包裹一层 动态的加上一些初始参数
本文代码
https://git.oschina.net/wangkang_daydayup/SpringBoot-Learn/tree/master
springboot-7
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之Servlet、Filter、Listener配置
- SpringBoot初始教程之Servlet、Filter、Listener配置
- springBoot配置servlet,filter,listener
- SpringBoot--添加配置Servlet,Filter,listener
- springboot注册servlet,Filter,Listener
- SpringBoot中Servlet&Listener&Filter的应用
- 在SpringBoot中使用 Servlet Filter Listener
- springboot 使用Servlet,Filter,Listener的方法
- spring boot学习8之Servlet/Filter/Listener配置
- SpringBoot 实战 (二) :web 综合应用(配置Servlet、Listener、Filter,Log)
- SpringBoot之Filter And Listener(D)
- springboot实战之注册filter和listener
- 第四十二章 SpringBoot注册Servlet/Filter/Listener
- Servlet Filter Servlet Listener
- Java Web之三 Servlet Filter Listener
- Spring Boot参考教程(六)Spring Boot配置Servlet,Filter,Listener,Interceptor
- IDEA中SonarLint的配置
- ajaxfileupload上传文件,返回json数据报错的问题。 Resource interpreted as Document but transferred with MIME type ap
- QT 取消ui->PushButton的边框
- mkdir -p
- public protected private区别
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- EXT3.4-gridpanel定时刷新滚动条自动滚动
- 利用Android Studio的 Monitor Memory 查找内存泄漏
- Java NIO 系列教程
- PHP数组实际占用内存大小的分析
- java连接sap系统调并调用用rfc函数
- Spring中的AOP(七)(实际上应该是一)——AspectJ的基本使用
- ThinkPhp框架:分页查询和补充框架知识
- 设计模式之单例模式