广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >玩转 SpringBoot 2 快速整合 Listener
  • 571
分享到

玩转 SpringBoot 2 快速整合 Listener

java教程SpringBoot 2017-03-26 02:03:57 571人浏览 无得
摘要

本文主要介绍如何在SpringBoot 2 中使用 Listener 的快速搭建教程,阅读前需要你必须了解 Listener 的基础使用以及如何搭建 springBoot 项目。快速演示操作第一步: 编写 Listener 并且在 List

本文主要介绍如何在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  userServlet(){
		return new ServletListenerRegistrationBean (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)

--结束END--

本文标题: 玩转 SpringBoot 2 快速整合 Listener

本文链接: https://www.lsjlt.com/news/4341.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作