玩转 SpringBoot 2 快速整合 Listener
前言
本文主要介绍如何在SpringBoot 2 中使用 Listener 的快速搭建教程,阅读前需要你必须了解 Listener 的基础使用以及如何搭建 SpringBoot 项目。
快速演示操作
第一步: 编写 Listener 并且在 Listener 类上声明 @WebListener 注解。具体代码如下:
@WebListener
public class ApplicationListener implements ServletContextListener{private Logger log = LoggerFactory.getLogger(ApplicationListener.class);@Overridepublic void contextInitialized(ServletContextEvent sce) {log.info("ApplicationListener 监听器启动...");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {log.info("ApplicationListener 监听器销毁...");}
}
第二步:通过 JavaConfig 方式将编写的 ApplicationListener 类注入到 Spring 的上下文中。
将自定义 ApplicationListener 传入到 ServletListenerRegistrationBean的构造中,然后创建 ServletListenerRegistrationBean Bean实例,具体代码如下:
@Configuration
public class WebApplicationConfig {@Beanpublic ServletListenerRegistrationBean<ApplicationListener> userServlet(){return new ServletListenerRegistrationBean<ApplicationListener> (new ApplicationListener());}
}
或者在启动类上声明 @ServletComponentScan 注解,具体代码如下:
@SpringBootApplication
@ServletComponentScan
public class SpringbootExamplesApplication {public static void main(String[] args) {SpringApplication.run(SpringbootExamplesApplication.class, args);}
}
测试
启动 SpirngBoot 项目会看到在 ApplicationListener 中定义 ApplicationListener 监听器销毁… 日志信息。
2019-10-04 00:58:39.361 INFO 5184 --- [ restartedMain] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2019-10-04 00:58:39.375 INFO 5184 --- [ restartedMain] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-10-04 00:58:39.376 INFO 5184 --- [ restartedMain] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-10-04 00:58:39.376 INFO 5184 --- [ restartedMain] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
2019-10-04 00:58:39.377 INFO 5184 --- [ restartedMain] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-10-04 00:58:39.420 INFO 5184 --- [ restartedMain] c.lijunkui.listener.ApplicationListener : ApplicationListener 监听器启动...
在启动状态下在此启动该项目,虽然会报错但是可以看到在ApplicationListener 中定义销毁的日志信息输出。
Caused by: java.net.BindException: Address already in use: bindat sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_144]at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_144]at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_144]at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_144]at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_144]at org.apache.tomcat.util.net.NioEndpoint.initServerSocket(NioEndpoint.java:236) ~[tomcat-embed-core-9.0.12.jar:9.0.12]at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-9.0.12.jar:9.0.12]at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1108) ~[tomcat-embed-core-9.0.12.jar:9.0.12]at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:550) ~[tomcat-embed-core-9.0.12.jar:9.0.12]at org.apache.catalina.connector.Connector.startInternal(Connector.java:957) ~[tomcat-embed-core-9.0.12.jar:9.0.12]... 19 common frames omitted2019-10-04 01:01:07.860 INFO 7864 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-10-04 01:01:07.863 INFO 7864 --- [ restartedMain] c.lijunkui.listener.ApplicationListener : ApplicationListener 监听器销毁...
2019-10-04 01:01:07.876 INFO 7864 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
小结
SpringBoot 中整合 Listener步骤如下:
- 需要在Listener上声明 @WebListener
- 在启动类上声明@ServletComponentScan注解或者将
Listener通过ServletListenerRegistrationBean 进行包装然后通过 JavaConfig
方式将其注入到Spring上下文中。
代码示例
我本地环境如下:
SpringBoot Version: 2.1.0.RELEASE
Apache Maven Version: 3.6.0
Java Version: 1.8.0_144
IDEA:Spring Tools Suite (STS)
整合过程如出现问题可以在我的GitHub 仓库 springbootexamples 中模块名为 spring-boot-2.x-listener 项目中进行对比查看
GitHub:https://github.com/zhuoqianmingyue/springbootexamples
参考文献
Springboot 系列(六)web 开发之拦截器和三大组件 By 雪漫士兵
玩转 SpringBoot 2 快速整合 Listener相关推荐
- 玩转 SpringBoot 2 快速整合 | RESTful Api 篇
概述 RESTful 是一种架构风格,任何符合 RESTful 风格的架构,我们都可以称之为 RESTful 架构.我们常说的 RESTful Api 是符合 RESTful 原则和约束的 HTTP ...
- 玩转 SpringBoot 2 快速整合 | JSP 篇
前言 JavaServer Pages(JSP)技术使Web开发人员和设计人员能够快速开发和轻松维护利用现有业务系统的信息丰富的动态Web页面.作为Java技术系列的一部分,JSP技术可以快速开发独立 ...
- 玩转 SpringBoot 2 快速整合 Filter 注解版
前言 本文主要介绍如何在SpringBoot 2 中使用 Filter 的快速搭建教程,阅读前需要你必须了解 Filter 的基础使用以及如何搭建 SpringBoot 项目. 快速演示操作 第一步: ...
- 玩转 SpringBoot 2 快速整合 Servlet
前言 本文主要介绍如何在SpringBoot 2 中使用 Servlet 的快速搭建教程,阅读前需要你必须了解 Servlet 的基础使用以及如何搭建 SpringBoot 项目. 快速演示操作 第一 ...
- 玩转 SpringBoot 2 快速整合拦截器
概述 首先声明一下,这里所说的拦截器是 SpringMVC 的拦截器(HandlerInterceptor).使用SpringMVC 拦截器需要做如下操作: 创建拦截器类需要实现 HandlerInt ...
- 玩转 SpringBoot 2 快速整合 | 丝袜哥(Swagger)
概述 首先让我引用 Swagger 官方的介绍: Design is the foundation of your API development. Swagger makes API design ...
- 玩转 SpringBoot 2 快速整合 | Thymeleaf 篇
前言 Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎. Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏览器中正确显示的HTML,也可以用 ...
- 玩转 SpringBoot 2 快速整合 Filter
概述 SpringBoot 中没有 web.xml, 我们无法按照原来的方式在 web.xml 中配置 Filter .但是我们可以通过 javaConfig(@Configuration +@Bea ...
- 玩转 SpringBoot 2 快速整合 | FreeMarker篇
FreeMarker 介绍 Apache FreeMarker™是一个模板引擎:一个Java库,用于根据模板和更改数据生成文本输出(HTML网页,电子邮件,配置文件,源代码等).模板是用FreeMar ...
最新文章
- [转]pragma comment的使用
- Java容器类研究4:ArrayList
- 在Java8的foreach()中不能break,如果需要continue时,可以使用return
- 计算机视觉课_计算机视觉教程—第4课
- 如何实现rtsp h265 转 rtmp (rtsp hevc 转 rtmp)并转发到CDN或自建服务器
- rsync同步工具学习笔记
- 征信报告上那些你不知道的事
- 乌班图运行perl脚本
- 四个vue后台常用模板,你用过几个?
- Android中英文切换
- 【机器学习】金融风控评分卡建模全流程!
- 工业生产管理-数据采集初探
- 瑞盟高精度模数转换器,MS1242,MS1243,
- pandas读取文件参数
- 程序人生 - 变脸的原理
- 大财配资平台靠谱吗?
- Java多维数组是什么,怎么用?
- IT忍者神龟之hibernate 延迟加载问题探讨
- 怎么录制教学视频?这两招解决你的录屏需求
- JS中map和foreach的区别以及some和every的用法
热门文章
- oracle集群服务删除,删除 Support for Oracle RAC
- OpenShift 之 用CodeReady Workspaces开发Quarkus云原生应用
- OpenShift 4 之配置Insecure Registry和Blocked Registry镜像源
- 使用ASP.NET Web API和Handlebars的Web模板
- 这次看到源码了,华为开源了方舟编译器
- SQL ——利用窗口函数的T-SQL解决方案
- 线程可以kill吗_还在用 kill -9 停机?这才是最优雅的姿势
- springboot+vue+element+mybatisplus项目(后端)
- url主机域名可以省略_接口自动化测试(三):关于URL
- python print format_Python中的format格式化输出