Spring中实现自定义事件

本文内容纲要: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();

}

}

测试结果:

Image

总结:

此实例中主要理解为:1、事件发布;2、事件监听;3、实现事件处理。

beans中主要是事件处理类的初始化和事件发布类的初始化。

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test15

本文内容总结:Spring中实现自定义事件

原文链接:https://www.cnblogs.com/EasonJim/p/6901706.html

以上是 Spring中实现自定义事件 的全部内容, 来源链接: utcz.com/z/296580.html

回到顶部