位置:首頁 > Java技術 > Spring > Spring自定義事件

Spring自定義事件

也有將要采取的編寫和發布自己的自定義事件的步驟。按照本章編寫,發布和處理自定義Spring 動作說明。

步驟 描述
1 Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. All the classes will be created under this package.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create an event class, CustomEvent by extending ApplicationEvent. This class must define a default constructor which should inherit constructor from ApplicationEvent class.
4 Once your event class is defined, you can publish it from any class, let us sayEventClassPublisher which implements ApplicationEventPublisherAware. You will also need to declare this class in XML configuration file as a bean so that the container can identify the bean as an event publisher because it implements the ApplicationEventPublisherAware interface.
5 A published event can be handled in a class, let us say EventClassHandler which implementsApplicationListener interface and implements onApplicationEvent method for the custom event.
6 Create beans configuration file Beans.xml under the src folder and a MainApp class which will work as Spring application.
7 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

這裡是CustomEvent.java文件的內容:

package com.yiibai;

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文件的內容:

package com.yiibai;

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文件的內容。

package com.yiibai;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{

   public void onApplicationEvent(CustomEvent event) {
      System.out.println(event.toString());
   }

}

以下是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");
	  
      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.yiibai.CustomEventHandler"/>

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

</beans>

創建源程序和bean配置文件完成後,讓我們運行應用程序。如果一切順利,這將打印以下信息:

My Custom Event
My Custom Event