本文主要介绍如何在SpringBoot 2 中使用 Listener 的快速搭建教程,阅读前需要你必须了解 Listener 的基础使用以及如何搭建 SpringBoot 项目。
快速演示操作
第一步: 编写 Listener 并且在 Listener 类上声明 @WebListener 注解。具体代码如下:
@WebListener public class ApplicationListener implements ServletContextListener{ private Logger log = LoggerFactory.getLogger(ApplicationListener.class); @Override public void contextInitialized(ServletContextEvent sce) { log.info("ApplicationListener 监听器启动..."); } @Override public void contextDestroyed(ServletContextEvent sce) { log.info("ApplicationListener 监听器销毁..."); } }
第二步:通过 JavaConfig 方式将编写的 ApplicationListener 类注入到 Spring 的上下文中。
将自定义 ApplicationListener 传入到 ServletListenerRegistrationBean的构造中,然后创建 ServletListenerRegistrationBean Bean实例,具体代码如下:
@Configuration public class WebApplicationConfig { @Bean public 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: bind at 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 omitted 2019-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)
以上是玩转 SpringBoot 2 快速整合 Listener的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

本文解释了用于构建分布式应用程序的Java的远程方法调用(RMI)。 它详细介绍了接口定义,实现,注册表设置和客户端调用,以解决网络问题和安全性等挑战。

本文详细介绍了用于网络通信的Java的套接字API,涵盖了客户服务器设置,数据处理和关键考虑因素,例如资源管理,错误处理和安全性。 它还探索了性能优化技术,我

本文详细介绍了创建自定义Java网络协议。 它涵盖协议定义(数据结构,框架,错误处理,版本控制),实现(使用插座),数据序列化和最佳实践(效率,安全性,维护


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。