Spring中實現自定義事件


原理:

  1. 通過擴展ApplicationEvent,創建一個事件類CustomEvent。這個類必須定義一個默認的構造函數,它應該從ApplicationEvent類中繼承的構造函數。
  2. 一旦定義事件類,你可以從任何類中發布它,假定EventClassPublisher實現了ApplicationEventPublisherAware。你還需要在XML配置文件中聲明這個類作為一個bean,之所以容器可以識別bean作為事件發布者,是因為它實現了ApplicationEventPublisherAware接口。
  3. 發布的事件可以在一個類中被處理,假定EventClassHandler實現了ApplicationListener接口,而且實現了自定義事件的onApplicationEvent方法。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jsoft.testspring</groupId>
  <artifactId>testcustomevent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testcustomevent</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency>
    
  </dependencies>
</project>

CustomEvent.java:

package com.jsoft.testspring.testcustomevent;

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {

    public CustomEvent(Object source) {
        super(source);
        // TODO Auto-generated constructor stub
    }
    
    public String toString(){
        return "My Custom Event";
    }

}

CustomEventPublisher.java:

package com.jsoft.testspring.testcustomevent;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class CustomEventPublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher publisher;
    
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        // TODO Auto-generated method stub
        this.publisher = applicationEventPublisher;
    }
    
    public void publish(){
        CustomEvent ce = new CustomEvent(this);
        publisher.publishEvent(ce);
    }

}

CustomEventHandler.java:

package com.jsoft.testspring.testcustomevent;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler implements ApplicationListener<CustomEvent> {

    @Override
    public void onApplicationEvent(CustomEvent event) {
        // TODO Auto-generated method stub
        System.out.println(event.toString());
    }

}

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.xsd">
        
    <bean id="customEventHandler" class="com.jsoft.testspring.testcustomevent.CustomEventHandler"></bean>
    
    <bean id="customEventPublisher" class="com.jsoft.testspring.testcustomevent.CustomEventPublisher"></bean>
</beans>

App.java:

package com.jsoft.testspring.testcustomevent;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        CustomEventPublisher publisher = (CustomEventPublisher)context.getBean("customEventPublisher");
        publisher.publish();
        publisher.publish();
    }
}

測試結果:

總結:

此實例中主要理解為:1、事件發布;2、事件監聽;3、實現事件處理。

beans中主要是事件處理類的初始化和事件發布類的初始化。

 

測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test15


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM