我是靠谱客的博主 沉静面包,这篇文章主要介绍玩转 SpringBoot 2 快速整合 Listener,现在分享给大家,希望可以做个参考。

本文主要介绍如何在SpringBoot 2 中使用 Listener 的快速搭建教程,阅读前需要你必须了解 Listener 的基础使用以及如何搭建 SpringBoot 项目。

快速演示操作

第一步: 编写 Listener 并且在 Listener 类上声明 @WebListener 注解。具体代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
@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实例,具体代码如下:

复制代码
1
2
3
4
5
6
7
@Configuration public class WebApplicationConfig { @Bean public ServletListenerRegistrationBean<ApplicationListener> userServlet(){ return new ServletListenerRegistrationBean<ApplicationListener> (new ApplicationListener()); } }
登录后复制

或者在启动类上声明 @ServletComponentScan 注解,具体代码如下:

复制代码
1
2
3
4
5
6
7
8
@SpringBootApplication @ServletComponentScan public class SpringbootExamplesApplication { public static void main(String[] args) { SpringApplication.run(SpringbootExamplesApplication.class, args); } }
登录后复制

测试

启动 SpirngBoot 项目会看到在 ApplicationListener 中定义 ApplicationListener 监听器销毁… 日志信息。

复制代码
1
2
3
4
5
6
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 中定义销毁的日志信息输出。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是沉静面包最近收集整理的关于玩转 SpringBoot 2 快速整合 Listener的全部内容,更多相关玩转内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部