Spring 中的自定義事件

2022-05-16 15:30 更新

Spring 中的自定義事件

編寫和發(fā)布自己的自定義事件有許多步驟。按照在這一章給出的說明來編寫,發(fā)布和處理自定義 Spring 事件。

步驟描述
1創(chuàng)建一個名稱為 SpringExample 的項目,并且在創(chuàng)建項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint。
2使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節(jié)。
3通過擴展 ApplicationEvent,創(chuàng)建一個事件類 CustomEvent。這個類必須定義一個默認的構(gòu)造函數(shù),它應該從 ApplicationEvent 類中繼承的構(gòu)造函數(shù)。
4一旦定義事件類,你可以從任何類中發(fā)布它,假定 EventClassPublisher 實現(xiàn)了 ApplicationEventPublisherAware。你還需要在 XML 配置文件中聲明這個類作為一個 bean,之所以容器可以識別 bean 作為事件發(fā)布者,是因為它實現(xiàn)了 ApplicationEventPublisherAware 接口。
5發(fā)布的事件可以在一個類中被處理,假定 EventClassHandler 實現(xiàn)了 ApplicationListener 接口,而且實現(xiàn)了自定義事件的 onApplicationEvent 方法。
6src 文件夾中創(chuàng)建 bean 的配置文件 Beans.xml 和 MainApp 類,它可以作為一個 Spring 應用程序來運行。
7最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容,并運行應用程序,解釋如下所示。

這個是 CustomEvent.java 文件的內(nèi)容:

package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent{ 
   public CustomEvent(Object source) {
      super(source);
   }
   public String toString(){
      return "My Custom Event";
   }
}

下面是 CustomEventPublisher.java 文件的內(nèi)容:

package com.tutorialspoint;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher 
   implements ApplicationEventPublisherAware {
   private ApplicationEventPublisher publisher;
   public void setApplicationEventPublisher
              (ApplicationEventPublisher publisher){
      this.publisher = publisher;
   }
   public void publish() {
      CustomEvent ce = new CustomEvent(this);
      publisher.publishEvent(ce);
   }
}

下面是 CustomEventHandler.java 文件的內(nèi)容:

package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{
   public void onApplicationEvent(CustomEvent event) {
      System.out.println(event.toString());
   }
}

下面是 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");    
      CustomEventPublisher cvp = 
      (CustomEventPublisher) context.getBean("customEventPublisher");
      cvp.publish();  
      cvp.publish();
   }
}

下面是配置文件 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="customEventHandler" 
      class="com.tutorialspoint.CustomEventHandler"/>

   <bean id="customEventPublisher" 
      class="com.tutorialspoint.CustomEventPublisher"/>

</beans>

一旦你完成了創(chuàng)建源和 bean 的配置文件后,我們就可以運行該應用程序。如果你的應用程序一切都正常,將輸出以下信息:

My Custom Event
My Custom Event


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號