1. 程式人生 > >Spring入門學習筆記(3)——事件處理類

Spring入門學習筆記(3)——事件處理類

aware super 不能 href his 應用 odi eap app

目錄

  • Spring中的事件處理
    • Spring內建事件
    • 監聽Context事件
    • Example
  • 自定義Spring事件

Spring中的事件處理

ApplicationContext 是Spring的核心模塊,管理著Beans完整的生命周期。當加載Bean時,ApplicationContext會發布特定類型的事件。
eg:當Context啟動時ContextStartEvent被發布,當關閉時,ContextStoppedEvent被發布。

ApplicationContext事件處理被ApplicationEvent類和ApplicationListener接口提供。因此,實現了ApplicationListener的bean,每次ApplicationContext發布ApplicationEvent時,Bean將會被通知。

Spring內建事件

  • ContextRefreshedEvent : 當ApplicationContext被初始化或者刷新時被發布。也可以通過調用ConfigurableApplicationContext接口的refresh()函數發起。
  • ContextStartedEvent : 當Application使用ConfigurableApplicationContext的start()方法啟動時被發布。您可以輪詢您的數據庫,也可以在收到此事件後重新啟動任何已停止的應用程序。
  • ContextStoppedEvent : 當ApplicationContext在ConfigurableApplicationContext接口上使用stop()方法停止時,就會發布這個事件。你可以在收到這個活動後做家務。
  • ContextClosedEvent : 當使用ConfigurableApplicationContext接口上的close()方法關閉ApplicationContext時,將發布此事件。一個封閉的環境到達了生命的終點;不能刷新或重新啟動。
  • RequestHandledEvent : 這是一個特定於web的事件,它告訴所有bean HTTP請求已經得到了服務。

Spring的事件處理是單線程的,因此如果發布了一個事件,直到並且除非所有接收者都得到消息,否則進程將被阻塞,線程將不會繼續。因此,如果要使用事件處理,那麽在設計應用程序時應該小心。

監聽Context事件

要想監聽一個context事件,bean需要實現僅有一個方法onApplicationEvent()的ApplicationListener接口

Example

HelloWorld.java

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

public class CStartEventHandler 
   implements ApplicationListener<ContextStartedEvent>{

   public void onApplicationEvent(ContextStartedEvent event) {
      System.out.println("ContextStartedEvent Received");
   }
}

CStopEventHandler

public class CStopEventHandler 
   implements ApplicationListener<ContextStoppedEvent>{

   public void onApplicationEvent(ContextStoppedEvent event) {
      System.out.println("ContextStoppedEvent Received");
   }
}

MainApp.java

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 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>

輸出:

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received

自定義Spring事件

下邊的案例將講述如何編寫和發布你自己的自定義事件

添加自定義事件CustomEvent.java,繼承ApplicationEvent類

public class CustomEvent extends ApplicationEvent{
   public CustomEvent(Object source) {
      super(source);
   }
   public String toString(){
      return "My Custom Event";
   }
}

添加自定義事件發布類CustomEventPublisher.java,實現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);
   }
}

自定義事件監聽處理類,實現ApplicationListener

public class CustomEventHandler implements ApplicationListener<CustomEvent> {
   public void onApplicationEvent(CustomEvent event) {
      System.out.println(event.toString());
   }
}

MainApp.java

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

<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>

註意不要忘記添加customEventHandler,雖然在主函數中沒有直接使用,但是context需要檢查實現了ApplicationListener的
bean,所以需要在xml文件中,添加該bean。

輸出:

my Custom Event
my Custom Event

Spring入門學習筆記(3)——事件處理類