- Spring Core 基础
- Spring - 首页
- Spring - 概述
- Spring - 架构
- Spring - 环境设置
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 作用域
- Spring - Bean 生命周期
- Spring - Bean 后处理器
- Spring - Bean 定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注解的配置
- Spring - 基于 Java 的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架进行 AOP
- Spring - JDBC 框架
- Spring - 事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- Spring 问题与解答
- Spring - 问题与解答
- Spring 有用资源
- Spring - 快速指南
- Spring - 有用资源
- Spring - 讨论
Spring 中的自定义事件
编写和发布您自己的自定义事件需要采取许多步骤。请按照本章中提供的说明来编写、发布和处理自定义 Spring 事件。
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中 src 文件夹下创建一个名为com.tutorialspoint的包。所有类都将在此包下创建。 |
| 2 | 使用添加外部 JAR选项添加所需的 Spring 库,如Spring Hello World 示例章节中所述。 |
| 3 | 创建一个事件类CustomEvent,继承自ApplicationEvent。此类必须定义一个默认构造函数,该构造函数应继承自 ApplicationEvent 类的构造函数。 |
| 4 | 定义好事件类后,您可以从任何类中发布它,例如EventClassPublisher,它实现了ApplicationEventPublisherAware。您还需要在 XML 配置文件中将此类声明为一个 Bean,以便容器可以识别该 Bean 为事件发布者,因为它实现了 ApplicationEventPublisherAware 接口。 |
| 5 | 已发布的事件可以在一个类中处理,例如EventClassHandler,它实现了ApplicationListener接口并为自定义事件实现了onApplicationEvent方法。 |
| 6 | 在src文件夹下创建 Bean 配置文件Beans.xml和一个MainApp类,它将用作 Spring 应用程序。 |
| 7 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并按如下所述运行应用程序。 |
以下是CustomEvent.java文件的内容
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文件的内容
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文件的内容
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文件的内容
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>
创建完源代码和 Bean 配置文件后,让我们运行应用程序。如果应用程序一切正常,它将打印以下消息:-
y Custom Event y Custom Event
广告