RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
如何玩转SpringBoot2快速整合Listener-创新互联

如何玩转 SpringBoot 2 快速整合 Listener?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联专注于漳州企业网站建设,响应式网站开发,商城系统网站开发。漳州网站建设公司,为漳州等地区提供建站服务。全流程定制制作,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

快速演示操作

第一步: 编写 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)

关于如何玩转 SpringBoot 2 快速整合 Listener问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章名称:如何玩转SpringBoot2快速整合Listener-创新互联
文章起源:http://scyingshan.cn/article/jipcp.html