SpringBoot初始教程之Servlet、Filter、Listener配置详解
发布时间 - 2026-01-11 03:07:08 点击率:次1.介绍

通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的
因为有可能打包之后是一个jar包的形式,这种情况下如何解决?SpringBoot 提供了两种方案进行解决
2.快速开始
2.1 方案一
方案一采用原生Servlet3.0的注解进行配置、@WebServlet 、@WebListener、@WebFilter是Servlet3.0 api中提供的注解
通过注解可以完全代替web.xml中的配置,下面是一个简单的配置
IndexServlet
@WebServlet(name = "IndexServlet",urlPatterns = "/hello")
public class IndexServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("hello word");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
IndexListener
@WebListener
public class IndexListener implements ServletContextListener {
private Log log = LogFactory.getLog(IndexListener.class);
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
log.info("IndexListener contextInitialized");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
IndexFilter
@WebFilter(urlPatterns = "/*", filterName = "indexFilter")
public class IndexFilter implements Filter {
Log log = LogFactory.getLog(IndexFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("init IndexFilter");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("doFilter IndexFilter");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
上面配置完了,需要配置一个核心的注解@ServletComponentScan,具体配置项如下,可以配置扫描的路径
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ServletComponentScanRegistrar.class)
public @interface ServletComponentScan {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
}
把注解加到入口处启动即可
@SpringBootApplication
@ServletComponentScan
public class AppApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(AppApplication.class, args);
}
}
2.2 方案二
方案二是采用自己SpringBoot 配置bean的方式进行配置的,SpringBoot提供了三种BeanFilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean 分别对应配置原生的Filter、Servlet、Listener,下面提供的三个配置和方案一采用的方式能够达到统一的效果
@Bean
public ServletRegistrationBean indexServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet());
registration.addUrlMappings("/hello");
return registration;
}
@Bean
public FilterRegistrationBean indexFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter());
registration.addUrlPatterns("/");
return registration;
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new IndexListener());
return servletListenerRegistrationBean;
}
3.总结
两种方案在使用上有差别,但是在内部SpringBoot的实现上是无差别的,即使使用的是Servlet3.0注解,也是通过扫描注解
转换成这三种bean的FilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean
4.扩展
大家在使用的时候有没有发觉,其实SpringBoot在使用SpringMvc的时候不需要配置DispatcherServlet的,因为已经自动配置了, 但是如果想要加一些初始配置参数如何解决,方案如下:
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("*.do");
registration.addUrlMappings("*.json");
return registration;
}
可以通过注入DispatcherServlet 然后用ServletRegistrationBean包裹一层 动态的加上一些初始参数
本文代码:springboot-Servlet-Filter-Listener_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# SpringBoot
# Servlet配置
# Filter配置
# Listener配置
# springboot注入servlet的方法
# SpringBoot中使用Filter和Interceptor的示例代码
# springboot使用filter获取自定义请求头的实现代码
# springboot中filter的用法详解
# springBoot的事件机制GenericApplicationListener用法解析
# 详解springboot整合Listener的两种方式
# SpringBoot中使用Servlet三大组件的方法(Servlet、Filter、Listene
# 是一个
# 两种
# 如何解决
# 的是
# 但是在
# 有可能
# 不需要
# 上有
# 可以通过
# 三种
# 转换成
# 中不
# 大家多多
# 这三种
# 情况下
# 无差别
# req
# HttpServletResponse
# flush
# HttpServletRequest
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
如何确保西部建站助手FTP传输的安全性?
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
如何在宝塔面板中创建新站点?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
Laravel的HTTP客户端怎么用_Laravel HTTP Client发起API请求教程
如何快速生成凡客建站的专业级图册?
5种Android数据存储方式汇总
Laravel如何实现多表关联模型定义_Laravel多对多关系及中间表数据存取【方法】
Laravel如何实现一对一模型关联?(Eloquent示例)
Python正则表达式进阶教程_复杂匹配与分组替换解析
网站优化排名时,需要考虑哪些问题呢?
Laravel如何使用缓存系统提升性能_Laravel缓存驱动和应用优化方案
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
大连 网站制作,大连天途有线官网?
活动邀请函制作网站有哪些,活动邀请函文案?
标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析
php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】
如何基于PHP生成高效IDC网络公司建站源码?
如何在阿里云完成域名注册与建站?
Laravel如何创建自定义Artisan命令?(代码示例)
HTML5打空格有哪些误区_新手常犯的空格使用错误【技巧】
UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】
如何在VPS电脑上快速搭建网站?
如何在万网利用已有域名快速建站?
如何用好域名打造高点击率的自主建站?
昵图网官方站入口 昵图网素材图库官网入口
JavaScript如何实现类型判断_typeof和instanceof有什么区别
Laravel如何使用查询构建器?(Query Builder高级用法)
如何批量查询域名的建站时间记录?
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
Android Socket接口实现即时通讯实例代码
Android GridView 滑动条设置一直显示状态(推荐)
使用C语言编写圣诞表白程序
黑客如何通过漏洞一步步攻陷网站服务器?
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
javascript和jQuery中的AJAX技术详解【包含AJAX各种跨域技术】
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
在线制作视频网站免费,都有哪些好的动漫网站?
如何确认建站备案号应放置的具体位置?
Laravel API资源(Resource)怎么用_格式化Laravel API响应的最佳实践
Laravel Eloquent性能优化技巧_Laravel N+1查询问题解决
Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面
制作电商网页,电商供应链怎么做?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
百度输入法全感官ai怎么关 百度输入法全感官皮肤关闭
Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理
如何快速使用云服务器搭建个人网站?
微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】
微信小程序 HTTPS报错整理常见问题及解决方案

