加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring(九)之事件处理

发布时间:2020-12-15 07:12:18 所属栏目:Java 来源:网络整理
导读:Spring的核心是 ApplicationContext ,它管理bean的完整生命周期。 ApplicationContext在加载bean时发布某些类型的事件。 例如, ContextStartedEvent 当上下文启动,并公布 ContextStoppedEvent 当上下文停止出版。 ApplicationContext 中的事件处理 是通过

Spring的核心是ApplicationContext,它管理bean的完整生命周期。ApplicationContext在加载bean时发布某些类型的事件。例如,ContextStartedEvent当上下文启动,并公布ContextStoppedEvent当上下文停止出版。

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

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

?

监听上下文事件

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

示例:

(1)编写HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   void setMessage(String message){
      this.message  = message;
   }
    getMessage(){
      System.out.println("Your Message : " + message);
   }
}

?

(2)编写CStartEventHandler.java

import org.springframework.context.ApplicationListener;
 org.springframework.context.event.ContextStartedEvent;

 CStartEventHandler 
   implements ApplicationListener<ContextStartedEvent>{

    onApplicationEvent(ContextStartedEvent event) {
      System.out.println("ContextStartedEvent Received");
   }
}

?

(3)编写CStopEventHandler.java

 org.springframework.context.event.ContextStoppedEvent;

 CStopEventHandler 
   implements ApplicationListener<ContextStoppedEvent> onApplicationEvent(ContextStoppedEvent event) {
      System.out.println("ContextStoppedEvent Received");
   }
}

?

(4)编写MainApp.java

 org.springframework.context.ConfigurableApplicationContext;
 org.springframework.context.support.ClassPathXmlApplicationContext;

 MainApp {
   static  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();
   }
}

?

(5)编写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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http:www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http:www.springframework.org/schema/context
    http:www.springframework.org/schema/context/spring-context-4.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>

?

(6)运行MainApp.java中的main方法,结果如下:

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读