你已經(jīng)看到了在所有章節(jié)中 Spring 的核心是 ApplicationContext,它負責(zé)管理 beans 的完整生命周期。當(dāng)加載 beans 時,ApplicationContext 發(fā)布某些類型的事件。例如,當(dāng)上下文啟動時,ContextStartedEvent 發(fā)布,當(dāng)上下文停止時,ContextStoppedEvent 發(fā)布。
通過 ApplicationEvent 類和 ApplicationListener 接口來提供在 ApplicationContext 中處理事件。如果一個 bean 實現(xiàn) ApplicationListener,那么每次 ApplicationEvent 被發(fā)布到 ApplicationContext 上,那個 bean 會被通知。
Spring 提供了以下的標(biāo)準(zhǔn)事件:
序號 | Spring 內(nèi)置事件 & 描述 |
---|---|
1 |
ContextRefreshedEvent ApplicationContext 被初始化或刷新時,該事件被發(fā)布。這也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法來發(fā)生。 |
2 |
ContextStartedEvent 當(dāng)使用 ConfigurableApplicationContext 接口中的 start() 方法啟動 ApplicationContext 時,該事件被發(fā)布。你可以調(diào)查你的數(shù)據(jù)庫,或者你可以在接受到這個事件后重啟任何停止的應(yīng)用程序。 |
3 |
ContextStoppedEvent 當(dāng)使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 時,發(fā)布這個事件。你可以在接受到這個事件后做必要的清理的工作。 |
4 |
ContextClosedEvent 當(dāng)使用 ConfigurableApplicationContext 接口中的 close() 方法關(guān)閉 ApplicationContext 時,該事件被發(fā)布。一個已關(guān)閉的上下文到達生命周期末端;它不能被刷新或重啟。 |
5 |
RequestHandledEvent 這是一個 web-specific 事件,告訴所有 bean HTTP 請求已經(jīng)被服務(wù)。 |
由于 Spring 的事件處理是單線程的,所以如果一個事件被發(fā)布,直至并且除非所有的接收者得到的該消息,該進程被阻塞并且流程將不會繼續(xù)。因此,如果事件處理被使用,在設(shè)計應(yīng)用程序時應(yīng)注意。
為了監(jiān)聽上下文事件,一個 bean 應(yīng)該實現(xiàn)只有一個方法 onApplicationEvent() 的 ApplicationListener 接口。因此,我們寫一個例子來看看事件是如何傳播的,以及如何可以用代碼來執(zhí)行基于某些事件所需的任務(wù)。
讓我們在恰當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個 Spring 應(yīng)用程序:
步驟 | 描述 |
---|---|
1 | 創(chuàng)建一個名稱為 SpringExample 的項目,并且在創(chuàng)建項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint。 |
2 | 使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節(jié)。 |
3 | 在 com.tutorialspoint 包中創(chuàng)建 Java 類 HelloWorld、CStartEventHandler、CStopEventHandler 和 MainApp。 |
4 | 在 src 文件夾中創(chuàng)建 Bean 的配置文件 Beans.xml。 |
5 | 最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容,并運行應(yīng)用程序,解釋如下所示。 |
這里是 HelloWorld.java 文件的內(nèi)容:
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 文件的內(nèi)容:
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 文件的內(nèi)容:
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 文件的內(nèi)容:
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>
一旦你完成了創(chuàng)建源和 bean 的配置文件,我們就可以運行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下消息:
ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received
更多建議: