本文主要介绍如何在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中文网其他相关文章!