Spring 中的事件处理
您已经在所有章节中看到,Spring 的核心是 ApplicationContext,它管理 bean 的完整生命周期。 ApplicationContext 在加载 bean 时发布某些类型的事件。 例如,ContextStartedEvent 在上下文启动时发布,ContextStoppedEvent 在上下文停止时发布。
ApplicationContext 中的事件处理是通过ApplicationEvent 类和ApplicationListener 接口提供的。 因此,如果 bean 实现了 ApplicationListener,那么每次 ApplicationEvent 发布到 ApplicationContext 时,都会通知该 bean。
Spring 提供了以下标准事件 −
| 序号 | Spring 内置事件 & 描述 |
|---|---|
| 1 |
ContextRefreshedEvent 此事件在 ApplicationContext 初始化或刷新时发布。 这也可以使用 ConfigurableApplicationContext 接口上的 refresh() 方法引发。 |
| 2 |
ContextStartedEvent 此事件在 ApplicationContext 使用 ConfigurableApplicationContext 接口上的 start() 方法启动时发布。 您可以轮询数据库,也可以在收到此事件后重新启动任何停止的应用程序。 |
| 3 |
ContextStoppedEvent 当使用 ConfigurableApplicationContext 接口上的 stop() 方法停止 ApplicationContext 时,将发布此事件。收到此事件后,您可以进行所需的 housekeep 工作。 |
| 4 |
ContextClosedEvent 此事件在使用 ConfigurableApplicationContext 接口上的 close() 方法关闭 ApplicationContext 时发布。一个封闭的上下文走到了生命的尽头; 它无法刷新或重新启动。 |
| 5 |
RequestHandledEvent 这是一个特定于 Web 的事件,告诉所有 bean 一个 HTTP 请求已被服务。 |
Spring 的事件处理是单线程的,所以如果一个事件被发布,除非所有接收者都收到消息,否则进程会被阻塞,流程将不会继续。 因此,如果要使用事件处理,在设计您的应用程序时应小心。
监听上下文事件
要监听上下文事件,bean 应该实现 ApplicationListener 接口,该接口只有一个方法 onApplicationEvent()。 所以让我们写一个例子来看看事件是如何传播的,以及如何让你的代码根据某些事件来完成所需的任务。
让我们有一个可以工作的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 −
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
| 2 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 3 | 在 com.tutorialspoint 包下创建 Java 类 HelloWorld、CStartEventHandler、CStopEventHandler 和 MainApp。 |
| 4 | 在 src 文件夹下创建 Beans 配置文件 Beans.xml。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,然后运行应用程序,如下所述。 |
这是 HelloWorld.java 文件的内容
package com.tutorialspoint;
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 文件的内容
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
以下是 CStopEventHandler.java 文件的内容
package com.tutorialspoint;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
以下是 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");
// 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
<?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>
完成创建源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 −
ContextStartedEvent Received Your Message : Hello World! ContextStoppedEvent Received
如果您愿意,您可以发布自己的自定义事件,稍后您可以捕获相同的事件以针对这些自定义事件采取任何操作。 如果您有兴趣编写自己的自定义事件,可以查看 Spring 中的自定义事件。

