首頁 >Java >java教程 >玩 SpringBoot 2 快速整合 Listener

玩 SpringBoot 2 快速整合 Listener

(*-*)浩
(*-*)浩轉載
2019-10-10 16:20:542388瀏覽

本文主要介紹如何在SpringBoot 2 中使用 Listener 的快速搭建教程,閱讀前需要你必須了解 Listener 的基礎使用以及如何搭建 SpringBoot 項目。

玩 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<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: &#39;characterEncodingFilter&#39; to: [/*]
2019-10-04 00:58:39.376  INFO 5184 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: &#39;hiddenHttpMethodFilter&#39; to: [/*]
2019-10-04 00:58:39.376  INFO 5184 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: &#39;formContentFilter&#39; to: [/*]
2019-10-04 00:58:39.377  INFO 5184 --- [  restartedMain] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: &#39;requestContextFilter&#39; 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中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除