目录

Spring - Event Handling in Spring

您已经在所有章节中看到Spring的核心是ApplicationContext ,它管理bean的完整生命周期。 ApplicationContext在加载bean时发布某些类型的事件。 例如,在启动上下文时发布ContextStoppedEvent ,在上下文停止时发布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的事件处理是单线程的,因此如果事件被发布,除非所有接收者都收到消息,否则流程将被阻止,流程将不会继续。 因此,如果要使用事件处理,在设计应用程序时应该小心。

听取上下文事件

要监听上下文事件,bean应该实现ApplicationListener接口,该接口只有一个onApplicationEvent()方法。 因此,让我们编写一个示例来查看事件如何传播以及如何使代码根据特定事件执行所需任务。

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3com.iowiki包下创建Java类HelloWorldCStartEventHandlerCStopEventHandlerMainApp
4src文件夹下创建Beans配置文件Beans.xml
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是HelloWorld.java文件的内容

package com.iowiki;
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.iowiki;
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.iowiki;
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.iowiki;
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.iowiki.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>
   <bean id = "cStartEventHandler" class = "com.iowiki.CStartEventHandler"/>
   <bean id = "cStopEventHandler" class = "com.iowiki.CStopEventHandler"/>
</beans>

完成源和bean配置文件的创建后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 -

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received

如果您愿意,可以发布自己的自定义事件,稍后您可以捕获相同的事件以对这些自定义事件采取任何操作。 如果您有兴趣编写自己的自定义事件,可以在Spring中查看自定义事件。

↑回到顶部↑
WIKI教程 @2018