`

Spring的ApplicationEvent的使用

 
阅读更多

Spring 3.0中提供了很多类似*Aware的类,其中ApplicationContextAware接口可以实现我们在初始化bean的时候给bean注入ApplicationContext(Spring上下文对象)对象。ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。因此,我们只需要构造好我们自己的ApplicationEvent和ApplicationListener,就可以在系统中实现相应的监听器。

   下面以增加学生的示例来演示如何构造Spring的监听器,StudentAddEvent是监听的事件对象,StudentAddListener是事件的监听器(负责处理接收到的监听事件),StudentAddBean负责触发StudentAddEvent事件。具体步骤如下:

1.  定义StudentAddEvent监听事件

新建StudentAddEvent类,实现抽象类

org.springframework.context.ApplicationEvent

StudentAddEvent类中需要实现自己的构造函数,具体代码如下:

  1. package com.trs.spring.event;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4.   
  5. /** 
  6.  * 增加学生的监听事件 
  7.  */  
  8. public class StudentAddEvent extends ApplicationEvent {  
  9.   
  10.     /** 
  11.      *  
  12.      */  
  13.     private static final long serialVersionUID = 20L;  
  14.   
  15.     /** 
  16.      * 学生姓名 
  17.      */  
  18.     private String m_sStudentName;  
  19.   
  20.     /** 
  21.      * @param source 
  22.      */  
  23.     public StudentAddEvent(Object source, String _sStudentName) {  
  24.         super(source);  
  25.         this.m_sStudentName = _sStudentName;  
  26.     }  
  27.   
  28.     /** 
  29.      * 获取学生姓名 
  30.      *  
  31.      * @return 
  32.      */  
  33.     public String getStudentName() {  
  34.         return m_sStudentName;  
  35.     }  
  36.   
  37. }    

2.  定义StudentAddListener监听器

新建StudentAddListener类,实现接口

org.springframework.context.ApplicationListener

中的onApplicationEvent方法,在该方法中只处理StudentAddEvent类型的ApplicationEvent事件,代码如下:

  1. package com.trs.spring.event;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4. import org.springframework.context.ApplicationListener;  
  5.   
  6. public class StudentAddListener implements ApplicationListener {  
  7.   
  8.     /* 
  9.      * (non-Javadoc) 
  10.      *  
  11.      * @see 
  12.      * org.springframework.context.ApplicationListener#onApplicationEvent(org 
  13.      * .springframework.context.ApplicationEvent) 
  14.      */  
  15.     public void onApplicationEvent(ApplicationEvent _event) {  
  16.         // 1.判断是否是增加学生对象的事件   
  17.         if (!(_event instanceof StudentAddEvent)) {  
  18.             return;  
  19.         }  
  20.   
  21.         // 2.是增加学生事件的对象,进行逻辑处理,比如记日志、积分等  
  22.         StudentAddEvent studentAddEvent = (StudentAddEvent) _event;  
  23.         System.out.println("增加了学生:::" + studentAddEvent.getStudentName());  
  24.     }  
  25.   
  26. }  

3.  定义StudentAddBean触发StudentAddEvent事件

新建StudentAddBean类,实现接口

org.springframework.context.ApplicationContextAware

中的setApplicationContext方法,在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件,具体代码如下:

  1. package com.trs.spring.event;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.ApplicationContextAware;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. public class StudentAddBean implements ApplicationContextAware {  
  9.     /** 
  10.      * 定义Spring上下文对象 
  11.      */  
  12.     private ApplicationContext m_applicationContext = null;  
  13.   
  14.     /* 
  15.      * (non-Javadoc) 
  16.      *  
  17.      * @see 
  18.      * org.springframework.context.ApplicationContextAware#setApplicationContext 
  19.      * (org.springframework.context.ApplicationContext) 
  20.      */  
  21.     public void setApplicationContext(ApplicationContext _applicationContext)  
  22.             throws BeansException {  
  23.         this.m_applicationContext = _applicationContext;  
  24.   
  25.     }  
  26.   
  27.     /** 
  28.      * 增加一个学生 
  29.      *  
  30.      * @param _sStudentName 
  31.      */  
  32.     public void addStudent(String _sStudentName) {  
  33.         // 1.构造一个增加学生的事件   
  34.         StudentAddEvent aStudentEvent = new StudentAddEvent(  
  35.                 m_applicationContext, _sStudentName);  
  36.         // 2.触发增加学生事件   
  37.         m_applicationContext.publishEvent(aStudentEvent);  
  38.     }  
  39.   
  40.     /** 
  41.      * @param args 
  42.      */  
  43.     public static void main(String[] args) {  
  44.         String[] xmlConfig = new String[] { "applicationContext.xml" };  
  45.         // 使用ApplicationContext来初始化系统  
  46.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  47.                 xmlConfig);  
  48.         StudentAddBean studentBean = (StudentAddBean) context  
  49.                 .getBean("StudentAddBean");  
  50.         studentBean.addStudent("我是第一个学生");  
  51.         studentBean.addStudent("第二个学生已经添加");  
  52.   
  53.     }  
  54.   
  55. }  

4.  applicationContext.xml配置文件

<bean id="StudentAddBean" class="com.trs.spring.event.StudentAddBean"></bean>

<bean id="StudentAddListener" class="com.trs.spring.event.StudentAddListener"></bean>

 

5.  说明

ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。

6.  执行StudentAddBean的main函数,结果如下:

增加了学生:::我是第一个学生

增加了学生:::第二个学生已经添加

分享到:
评论

相关推荐

    Spring的ApplicationEvent事件和监听器的测试Demo

    Spring的ApplicationEvent事件和监听器的测试工程,演示了如何使用Spring中的事件和监听器内容。完整的maven工程,能够运行。

    Spring事件Application Event原理详解

    主要介绍了Spring 事件Application Event原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Java Spring框架面试题大全

    Spring使用ApplicationEvent和ApplicationListener两个核心接口实现了事件监听机制。开发者可以自定义事件类和事件监听器,并通过应用程序上下文的publishEvent方法来发布事件,Spring框架会自动分发事件给所有对该...

    SpringEventTest工程

    Spring的ApplicationEvent事件和监听器的测试工程,演示了如何使用Spring中的事件和监听器内容。

    深入探索Spring事件监听机制:技术与应用

    它们可以通过实现ApplicationListener接口或使用@EventListener注解来定义。这些监听器关注特定类型的事件,并在事件发生时执行定义的逻辑。Spring的事件机制允许一个事件被多个监听器监听,增加了灵活性和可扩展性...

    spring-resource:spring resouce code learn(Spring源码学习)

    spring-resourcespring resouce code learn自己学习Spring过程中的一些记录, 太复杂的功能记录大概流程, 一些重要点注释实现细节和补充一些使用的例子spring 源码阅读记录IOCAOPMVCSpringMVC源码分析spring 实战...

    SPRING API 2.0.CHM

    ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ApplicationEventPublisherAware ApplicationListener ApplicationObjectSupport ArgPreparedStatementSetter ...

    springboot学习思维笔记.xmind

    自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware BeanNameAware BeanFactoryAware ApplicationContextAware ...

    SpringBoot事件发布及订阅详解含代码示例(值得珍藏)

    ApplicationEvent 是 Spring 框架中的一个重要概念,它是基于观察者模式的事件。简单来说,它是一个用于在 Spring 应用程序上下文中传播信息的对象。当某个特定的事件发生时,ApplicationEvent 对象会被创建并发布到...

    applicationEvent实战demo

    用户需要通过邮箱验证码来登录,直接的做法无非是直接在项目中的登录服务里调用邮件服务,通过应用ApplicationEvent可以对这样一个调用解耦,改为在登录服务中发布一个邮箱登录事件,Listener监听后发送邮件。

    详解SpringBoot 发布ApplicationEventPublisher和监听ApplicationEvent事件

    主要介绍了详解SpringBoot 发布ApplicationEventPublisher和监听ApplicationEvent事件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring活动

    Spring活动 有一些简单的准则可以遵循: 事件应扩展ApplicationEvent,发布者应注入ApplicationEventPublisher对象,侦听器应实现ApplicationListener接口 事件-默认情况下-是同步的。 这具有一些优点-例如,侦听器...

    springboot.zip

    SpringBoot 中发布ApplicationEventPublisher,监听ApplicationEvent 异步线程池操作

    03-05-12-ApacheDubbo服务发布源码分析1

    1.ApplicationEvent:表示事件本身,自定义事件需要继承该类 2.ApplicationEventPublisherAware:事件发送器,需要实

Global site tag (gtag.js) - Google Analytics