📜  Spring中的事件处理

📅  最后修改于: 2020-11-11 07:05:17             🧑  作者: Mango


在所有章节中,您都已经看到Spring的核心是ApplicationContext ,它管理着bean的整个生命周期。加载bean时,ApplicationContext发布某些类型的事件。例如,当上下文开始,当上下文停止ContextStoppedEvent被出版ContextStartedEvent出版。

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

Spring提供以下标准事件-

Sr.No. Spring Built-in Events & Description
1

ContextRefreshedEvent

This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface.

2

ContextStartedEvent

This event is published when the ApplicationContext is started using the start() method on the ConfigurableApplicationContext interface. You can poll your database or you can restart any stopped application after receiving this event.

3

ContextStoppedEvent

This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event.

4

ContextClosedEvent

This event is published when the ApplicationContext is closed using the close() method on the ConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted.

5

RequestHandledEvent

This is a web-specific event telling all beans that an HTTP request has been serviced.

Spring的事件处理是单线程的,因此,如果事件被发布,则直到并且除非所有接收者都收到消息,否则流程将被阻塞且流程将不会继续。因此,如果要使用事件处理,则在设计应用程序时应格外小心。

听上下文事件

要侦听上下文事件,bean应该实现ApplicationListener接口,该接口只有一个方法onApplicationEvent() 。因此,让我们编写一个示例,以了解事件如何传播以及如何根据某些事件将代码放入执行所需任务的过程。

让我们拥有一个运行良好的Eclipse IDE,并采取以下步骤来创建一个Spring应用程序:

Step Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, CStartEventHandler, CStopEventHandler and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这是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{

   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{

   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





   
      
   

   
   


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

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received

如果愿意,您可以发布自己的自定义事件,以后再捕获它们,以对这些自定义事件采取任何措施。如果您有兴趣编写自己的自定义事件,则可以在Spring中选中“自定义事件”。