溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何玩轉(zhuǎn) SpringBoot 2 快速整合 Listener

發(fā)布時(shí)間:2020-07-15 09:16:07 來源:億速云 閱讀:225 作者:Leah 欄目:編程語言

如何玩轉(zhuǎn) SpringBoot 2 快速整合 Listener?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

快速演示操作

第一步: 編寫 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 監(jiān)聽器啟動(dòng)...");
	}
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		log.info("ApplicationListener 監(jiān)聽器銷毀...");
	}
}

第二步:通過 JavaConfig 方式將編寫的 ApplicationListener 類注入到 Spring 的上下文中。

將自定義 ApplicationListener 傳入到 ServletListenerRegistrationBean的構(gòu)造中,然后創(chuàng)建 ServletListenerRegistrationBean Bean實(shí)例,具體代碼如下:

@Configuration
public class WebApplicationConfig {
	@Bean
	public ServletListenerRegistrationBean<ApplicationListener>  userServlet(){
		return new ServletListenerRegistrationBean<ApplicationListener> (new ApplicationListener());
	}
}

或者在啟動(dòng)類上聲明 @ServletComponentScan 注解,具體代碼如下:

@SpringBootApplication
@ServletComponentScan
public class SpringbootExamplesApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootExamplesApplication.class, args);
	}
}

測(cè)試

啟動(dòng) SpirngBoot 項(xiàng)目會(huì)看到在 ApplicationListener 中定義 ApplicationListener 監(jiān)聽器銷毀… 日志信息。

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 監(jiān)聽器啟動(dòng)...

在啟動(dòng)狀態(tài)下在此啟動(dòng)該項(xiàng)目,雖然會(huì)報(bào)錯(cuò)但是可以看到在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 監(jiān)聽器銷毀...
2019-10-04 01:01:07.876  INFO 7864 --- [  restartedMain] ConditionEvaluationReportLoggingListener :

小結(jié)

SpringBoot 中整合 Listener步驟如下:

需要在Listener上聲明 @WebListener

在啟動(dòng)類上聲明@ServletComponentScan注解或者將

Listener通過ServletListenerRegistrationBean 進(jìn)行包裝然后通過 JavaConfig

方式將其注入到Spring上下文中。

代碼示例

我本地環(huán)境如下:

SpringBoot Version: 2.1.0.RELEASE

Apache Maven Version: 3.6.0

Java Version: 1.8.0_144

IDEA:Spring Tools Suite (STS)

關(guān)于如何玩轉(zhuǎn) SpringBoot 2 快速整合 Listener問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI