Spring事件處理
你已經看到了所有的章節了Spring的核心是ApplicationContext,它管理bean的整個生命周期。ApplicationContext中加載bean時發布某些類型的事件。例如,一個ContextStartedEvent是當所述上下文被啟動並發布,上下文被停止ContextStoppedEvent。
事件處理中ApplicationContext 通過了ApplicationEvent類和ApplicationListener接口提供。所以,如果一個bean實現了ApplicationListener,然後每anApplicationEvent被發布到ApplicationContext的時候通知bean。
Spring提供了以下標準的事件:
S.N. | Spring 內置活動及說明 |
---|---|
1 |
ContextRefreshedEvent This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface. |
2 |
ContextStartedEvent This event is published when the ApplicationContext is started using the start() method on theConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event. |
3 |
ContextStoppedEvent This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event. |
4 |
ContextClosedEvent This event is published when the ApplicationContext is closed using the close() method on theConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted. |
5 |
RequestHandledEvent This is a web-specific event telling all beans that an HTTP request has been serviced. |
Spring事件處理是單線程的,所以如果一個事件被發布,直至及除非所有的接收器得到的消息,該進程被阻塞並且流程將不會繼續。因此,應注意在設計應用程序時,如果事件處理被使用。
監聽上下文事件:
要監聽上下文事件,一個bean應該實現ApplicationListener接口,隻有一個方法onApplicationEvent()。因此,讓我們寫一個例子,看看如何傳播的事件,以及如何可以把代碼來執行基於某些事件所需的任務。
讓我們使用Eclipse IDE,然後按照下麵的步驟來創建一個Spring應用程序:
步驟 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld, CStartEventHandler, CStopEventHandler and MainAppunder the com.yiibai package. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
這裡是HelloWorld.java 文件的內容:
package com.yiibai; 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.yiibai; 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.yiibai; 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.yiibai; 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.yiibai.HelloWorld"> <property name="message" value="Hello World!"/> </bean> <bean id="cStartEventHandler" class="com.yiibai.CStartEventHandler"/> <bean id="cStopEventHandler" class="com.yiibai.CStopEventHandler"/> </beans>
創建源程序和bean配置文件完成,讓我們運行應用程序。如果一切順利,這將打印以下信息:
ContextStartedEvent Received Your Message : Hello World! ContextStoppedEvent Received
如果喜歡,你可以發布自己的自定義事件,以後你可以捕捉相同處理那些自定義事件動作。如果你有興趣在編寫自己的自定義事件,可以查看 Spring自定義事件