Spring 中的事件處理

2022-05-16 15:29 更新

Spring 中的事件處理

你已經(jīng)看到了在所有章節(jié)中 Spring 的核心是 ApplicationContext,它負(fù)責(zé)管理 beans 的完整生命周期。當(dāng)加載 beans 時(shí),ApplicationContext 發(fā)布某些類型的事件。例如,當(dāng)上下文啟動(dòng)時(shí),ContextStartedEvent 發(fā)布,當(dāng)上下文停止時(shí),ContextStoppedEvent 發(fā)布。

通過 ApplicationEvent 類和 ApplicationListener 接口來提供在 ApplicationContext 中處理事件。如果一個(gè) bean 實(shí)現(xiàn) ApplicationListener,那么每次 ApplicationEvent 被發(fā)布到 ApplicationContext 上,那個(gè) bean 會(huì)被通知。

Spring 提供了以下的標(biāo)準(zhǔn)事件:

序號(hào) Spring 內(nèi)置事件 & 描述
1

ContextRefreshedEvent

ApplicationContext 被初始化或刷新時(shí),該事件被發(fā)布。這也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法來發(fā)生。

2

ContextStartedEvent

當(dāng)使用 ConfigurableApplicationContext 接口中的 start() 方法啟動(dòng) ApplicationContext 時(shí),該事件被發(fā)布。你可以調(diào)查你的數(shù)據(jù)庫,或者你可以在接受到這個(gè)事件后重啟任何停止的應(yīng)用程序。

3

ContextStoppedEvent

當(dāng)使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 時(shí),發(fā)布這個(gè)事件。你可以在接受到這個(gè)事件后做必要的清理的工作。

4

ContextClosedEvent

當(dāng)使用 ConfigurableApplicationContext 接口中的 close() 方法關(guān)閉 ApplicationContext 時(shí),該事件被發(fā)布。一個(gè)已關(guān)閉的上下文到達(dá)生命周期末端;它不能被刷新或重啟。

5

RequestHandledEvent

這是一個(gè) web-specific 事件,告訴所有 bean HTTP 請(qǐng)求已經(jīng)被服務(wù)。

由于 Spring 的事件處理是單線程的,所以如果一個(gè)事件被發(fā)布,直至并且除非所有的接收者得到的該消息,該進(jìn)程被阻塞并且流程將不會(huì)繼續(xù)。因此,如果事件處理被使用,在設(shè)計(jì)應(yīng)用程序時(shí)應(yīng)注意。

監(jiān)聽上下文事件

為了監(jiān)聽上下文事件,一個(gè) bean 應(yīng)該實(shí)現(xiàn)只有一個(gè)方法 onApplicationEvent() 的 ApplicationListener 接口。因此,我們寫一個(gè)例子來看看事件是如何傳播的,以及如何可以用代碼來執(zhí)行基于某些事件所需的任務(wù)。

讓我們?cè)谇‘?dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟 描述
1 創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目,并且在創(chuàng)建項(xiàng)目的 src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint。
2 使用 Add External JARs 選項(xiàng),添加所需的 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ùn)行應(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ùn)行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,將輸出以下消息:

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)