
- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境設定
- Spring - HelloWorld示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 注入集合
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - 使用Spring框架的AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring中的事件處理
在所有章節中,您都看到了Spring的核心是ApplicationContext,它管理Bean的完整生命週期。ApplicationContext在載入Bean時釋出某些型別的事件。例如,當上下文啟動時釋出ContextStartedEvent,當上下文停止時釋出ContextStoppedEvent。
ApplicationContext中的事件處理是透過ApplicationEvent類和ApplicationListener介面提供的。因此,如果一個Bean實現了ApplicationListener,那麼每次將ApplicationEvent釋出到ApplicationContext時,都會通知該Bean。
Spring提供以下標準事件:
序號 | Spring內建事件及描述 |
---|---|
1 |
ContextRefreshedEvent 當ApplicationContext被初始化或重新整理時釋出此事件。這也可以使用ConfigurableApplicationContext介面上的refresh()方法觸發。 |
2 |
ContextStartedEvent 使用ConfigurableApplicationContext介面上的start()方法啟動ApplicationContext時釋出此事件。收到此事件後,您可以輪詢資料庫或重新啟動任何已停止的應用程式。 |
3 |
ContextStoppedEvent 使用ConfigurableApplicationContext介面上的stop()方法停止ApplicationContext時釋出此事件。收到此事件後,您可以執行必要的清理工作。 |
4 |
ContextClosedEvent 使用ConfigurableApplicationContext介面上的close()方法關閉ApplicationContext時釋出此事件。關閉的上下文將達到其生命週期結束;它無法重新整理或重新啟動。 |
5 |
RequestHandledEvent 這是一個特定於Web的事件,它告訴所有Bean已處理HTTP請求。 |
Spring的事件處理是單執行緒的,因此如果釋出了一個事件,除非所有接收者都收到訊息,否則程序將被阻塞,並且流程將不會繼續。因此,如果要使用事件處理,則在設計應用程式時應注意。
監聽上下文事件
要監聽上下文事件,Bean應該實現ApplicationListener介面,該介面只有一個方法onApplicationEvent()。讓我們編寫一個示例來檢視事件如何傳播以及如何根據某些事件編寫程式碼來執行所需的任務。
讓我們準備好一個可用的Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
步驟 | 描述 |
---|---|
1 | 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個com.tutorialspoint包。 |
2 | 如Spring HelloWorld示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。 |
3 | 在com.tutorialspoint包下建立Java類HelloWorld、CStartEventHandler、CStopEventHandler和MainApp。 |
4 | 在src資料夾下建立Bean配置檔案Beans.xml。 |
5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按如下所述執行應用程式。 |
以下是HelloWorld.java檔案的內容
package com.tutorialspoint; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
以下是CStartEventHandler.java檔案的內容
package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStartedEvent; public class CStartEventHandler implements ApplicationListener<ContextStartedEvent>{ public void onApplicationEvent(ContextStartedEvent event) { System.out.println("ContextStartedEvent Received"); } }
以下是CStopEventHandler.java檔案的內容
package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event) { System.out.println("ContextStoppedEvent Received"); } }
以下是MainApp.java檔案的內容
package com.tutorialspoint; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); // Let us raise a start event. context.start(); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); // Let us raise a stop event. context.stop(); } }
以下是配置檔案Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"> <property name = "message" value = "Hello World!"/> </bean> <bean id = "cStartEventHandler" class = "com.tutorialspoint.CStartEventHandler"/> <bean id = "cStopEventHandler" class = "com.tutorialspoint.CStopEventHandler"/> </beans>
建立原始檔和Bean配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
ContextStartedEvent Received Your Message : Hello World! ContextStoppedEvent Received
如果您願意,您可以釋出您自己的自定義事件,稍後您可以捕獲相同的事件以對這些自定義事件採取任何操作。如果您有興趣編寫您自己的自定義事件,您可以檢視Spring中的自定義事件。