Spring 简明教程

Event Handling in Spring

您已在所有章节中了解到 Spring 的核心是 ApplicationContext ,它管理 bean 的完整生命周期。ApplicationContext 在加载 bean 时会发布特定类型的事件。例如,当上下文启动时,将发布 ContextStartedEvent,当上下文停止时,将发布 ContextStoppedEvent。

ApplicationContext 中的事件处理通过 ApplicationEvent 类和 ApplicationListener 接口来提供。因此,如果一个 bean 实现了 ApplicationListener,那么每次一个 ApplicationEvent 被发布到 ApplicationContext 时,该 bean 都会收到通知。

Spring 提供了以下标准事件 -

Sr.No.

Spring 内置事件和描述

1

ContextRefreshedEvent 此事件在 ApplicationContext 被初始化或刷新时发布。也可以使用 ConfigurableApplicationContext 接口上的 refresh() 方法触发此事件。

2

ContextStartedEvent 此事件在使用 ConfigurableApplicationContext 接口上的 start() 方法启动 ApplicationContext 时发布。您可以在收到此事件后轮询您的数据库或重新启动任何已停止的应用程序。

3

ContextStoppedEvent 此事件在使用 ConfigurableApplicationContext 接口上的 stop() 方法停止 ApplicationContext 时发布。您可以在收到此事件后执行所需的维护工作。

4

ContextClosedEvent 此事件在使用 ConfigurableApplicationContext 接口上的 close() 方法关闭 ApplicationContext 时发布。关闭的上下文已达到其生命周期结束;不能刷新或重新启动它。

5

RequestHandledEvent 这是一个特定于 Web 的事件,它告诉所有 bean 一个 HTTP 请求已经被服务。

Spring 的事件处理是单线程的,因此如果发布一个事件,直到所有接收者收到消息,进程才会被阻止,并且流程不会继续。因此,在设计需要使用事件处理的应用程序时应小心。

Listening to Context Events

为了监听上下文事件,bean 应实现 ApplicationListener 接口,该接口只有一个方法 onApplicationEvent() 。因此,让我们写一个示例来看一下事件如何传播,以及您如何将代码放在那里,以便根据某些事件执行所需的任务。

让我们准备一个可用的 Eclipse IDE,然后执行以下步骤来创建一个 Spring 应用程序−

Step

Description

1

使用 SpringExample 创建一个项目,并在创建的项目 src 文件夹下创建 com.tutorialspoint 包。

2

使用 Add External JARs 选项添加必需的 Spring 库,如 Spring Hello World Example 章节中所述。

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

如果您愿意,您可以发布自己的自定义事件,稍后您可以获取这些事件并对它们执行任何操作。如果您有兴趣编写自己的自定义事件,可以检查 Custom Events in Spring.