spring事件机制_http://enjiex.iteye.com/blog/1070094

news/2024/5/19 14:36:03
Java提供了事件机制,在使用spring的时候,我们可以把普通的java事件操作定义为bean集成到bean容器中,但还有一种更方便的方式,即使用spring已集成的事件支持。
在使用Spring的事件支持时,我们需要关注以下几个对象:
      1. ApplicationEvent:继承自EventObject,同时是spring的application中事件的父类,需要被自定义的事件继承。
      2. ApplicationListener:继承自EventListener,spring的application中的监听器必须实现的接口,需要被自定义的监听器实现其onApplicationEvent方法
      3. ApplicationEventPublisherAware:在spring的context中希望能发布事件的类必须实现的接口,该接口中定义了设置ApplicationEventPublisher的方法,由ApplicationContext调用并设置。在自己实现的ApplicationEventPublisherAware子类中,需要有ApplicationEventPublisher属性的定义。
      4. ApplicationEventPublisher:spring的事件发布者接口,定义了发布事件的接口方法publishEvent。因为ApplicationContext实现了该接口,因此spring的ApplicationContext实例具有发布事件的功能(publishEvent方法在AbstractApplicationContext中有实现)。在使用的时候,只需要把ApplicationEventPublisher的引用定义到ApplicationEventPublisherAware的实现中,spring容器会完成对ApplicationEventPublisher的注入。

在spring的bean配置中,因为事件是由事件源发出的,不需要注册为bean由spring容器管理。所以在spring的配置中,只需配置自定义的ApplicationEventListener和publisherAware(即实现了ApplicationEventPublisherAware接口的发布类),而对于ApplicationEventPublisher的管理和注入都由容器来完成。

基于spring的事件简单实现如下:
1. 定义ApplicationEvent
Java代码   收藏代码
  1. package com.nuc.event;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4. /** 
  5.  * 定义Spring容器中的事件,与java普通的事件定义相比,只是继承的父类不同而已,在 
  6.  * 在定义上并未有太大的区别,毕竟ApplicationEvent也是继承自EventObject的。 
  7.  */  
  8. public class MethodExecutionEvent extends ApplicationEvent {  
  9.   
  10.     private static final long serialVersionUID = 2565706247851725694L;  
  11.     private String methodName;  
  12.     private MethodExecutionStatus methodExecutionStatus;  
  13.       
  14.     public MethodExecutionEvent(Object source) {  
  15.         super(source);  
  16.     }  
  17.       
  18.     public MethodExecutionEvent(Object source, String methodName, MethodExecutionStatus methodExecutionStatus) {  
  19.         super(source);  
  20.         this.methodName = methodName;  
  21.         this.methodExecutionStatus = methodExecutionStatus;  
  22.     }  
  23.   
  24.     public String getMethodName() {  
  25.         return methodName;  
  26.     }  
  27.   
  28.     public void setMethodName(String methodName) {  
  29.         this.methodName = methodName;  
  30.     }  
  31.   
  32.     public MethodExecutionStatus getMethodExecutionStatus() {  
  33.         return methodExecutionStatus;  
  34.     }  
  35.   
  36.     public void setMethodExecutionStatus(MethodExecutionStatus methodExecutionStatus) {  
  37.         this.methodExecutionStatus = methodExecutionStatus;  
  38.     }  
  39. }  

2. 定义ApplicationEventListener
Java代码   收藏代码
  1. package com.nuc.listener;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4. import org.springframework.context.ApplicationListener;  
  5.   
  6. import com.nuc.event.MethodExecutionEvent;  
  7. import com.nuc.event.MethodExecutionStatus;  
  8. /** 
  9.  * Spring容器中的事件监听器,与java中基本的事件监听器的定义相比,这里需要实现ApplicationListener接口 
  10.  * ApplicationListener接口虽然继承自EventListener,但扩展了EventListener 
  11.  * 它在接口声明中定义了onApplicationEvent的接口方法,而不像EventListener只作为标记性接口。 
  12.  */  
  13.   
  14. public class MethodExecutionEventListener implements ApplicationListener {  
  15.   
  16.     public void onApplicationEvent(ApplicationEvent event) {  
  17.         if (event instanceof MethodExecutionEvent) {  
  18.            if (MethodExecutionStatus.BEGIN  
  19.                     .equals(((MethodExecutionEvent) event)  
  20.                             .getMethodExecutionStatus())) {  
  21.                 System.out.println("It's beginning");  
  22.             }  
  23.             if (MethodExecutionStatus.END.equals(((MethodExecutionEvent) event).getMethodExecutionStatus())) {  
  24.                 System.out.println("It's ending");  
  25.             }  
  26.         }  
  27.     }  
  28. }  

3. 定义ApplicationEventPublisherAware
Java代码   收藏代码
  1. package com.nuc.publisher;  
  2.   
  3. import org.springframework.context.ApplicationEventPublisher;  
  4. import org.springframework.context.ApplicationEventPublisherAware;  
  5.   
  6. import com.nuc.event.MethodExecutionEvent;  
  7. import com.nuc.event.MethodExecutionStatus;  
  8.   
  9. public class MethodExecutionEventPublisher implements  
  10.         ApplicationEventPublisherAware {  
  11.   
  12.     private ApplicationEventPublisher eventPublisher;  
  13.       
  14.     public void methodToMonitor() {  
  15.         MethodExecutionEvent beginEvent = new MethodExecutionEvent(this"methodToMonitor", MethodExecutionStatus.BEGIN);  
  16.         this.eventPublisher.publishEvent(beginEvent);  
  17.         //TODO  
  18.         MethodExecutionEvent endEvent = new MethodExecutionEvent(this"methodToMonitor", MethodExecutionStatus.END);  
  19.         this.eventPublisher.publishEvent(endEvent);  
  20.     }  
  21.       
  22.     public void setApplicationEventPublisher(  
  23.             ApplicationEventPublisher applicationEventPublisher) {  
  24.         this.eventPublisher = applicationEventPublisher;  
  25.     }  
  26. }  

4. 定义bean配置
Java代码   收藏代码
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"  
  5.     default-autowire="byName">  
  6.     <bean id="methodExecListener" class="com.nuc.listener.MethodExecutionEventListener"></bean>  
  7.     <bean id="evtPublisher" class="com.nuc.publisher.MethodExecutionEventPublisher"></bean>   
  8. </beans>  

5. 使用事件
Java代码   收藏代码
  1. package com.nuc;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.nuc.publisher.MethodExecutionEventPublisher;  
  7.   
  8. public class App   
  9. {  
  10.     public static void main( String[] args )  
  11.     {  
  12.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  13.         MethodExecutionEventPublisher publisher = (MethodExecutionEventPublisher)context.getBean("evtPublisher");  
  14.         publisher.methodToMonitor();  
  15.     }  
  16. }  


从实现来上,和使用基本的java事件流程大体一致,不同的是这里不需要自定义发布者,而使用spring实现提供的事件发布实现。当然,这里却引入了ApplicationEventPublisherAware。

http://www.niftyadmin.cn/n/2008624.html

相关文章

解决Spring Cloud中Feign/Ribbon第一次请求失败的方法___http://www.jb51.net/article/106944.htm

这篇文章主要给大家介绍了关于解决Spring Cloud中Feign/Ribbon第一次请求失败的方法&#xff0c;文中给出了三种解决的方法&#xff0c;大家可以根据需要选择对应的方法&#xff0c;需要的朋友们下面来一起看看吧。前言 在Spring Cloud中&#xff0c;Feign和Ribbon在整合了Hyst…

行内元素有哪些?块级元素有哪些? 空(void)元素有那些?

首先&#xff1a;CSS规范规定&#xff0c;每个元素都有display属性&#xff0c;确定该元素的类型&#xff0c;每个元素都有默认的display值&#xff0c;如div的display默认值为“block”&#xff0c;则为“块级”元素&#xff1b;span默认display属性值为“inline”&#xff0c…

windows10安装nodeJs及环境配置

学习于https://www.cnblogs.com/zhouy...

微信nickname乱码(emoji)及mysql编码格式设置(utf8mb4)解决的过程__https://segmentfault.com/a/1190000004594385

自己的练习项目中涉及保存微信的nickname&#xff0c;之前一直正常使用&#xff0c;但是突然遇到一个之前没有遇到的问题。经过调试发现错误如下&#xff1a;Incorrect string value: \xF0\x9F\x99\x88\xF0\x9F... for column nickname at row 1 经过仔细查看发现可以获得nickn…

反编译java class并优雅的调试--http://www.blogjava.net/miaoyachun/archive/2013/02/22/395575.html

https://sourceforge.net/projects/realignmentjd/files/ 官方文档 用jd-eclipse 插件来反编译java class文件的输出还是挺nice的&#xff0c;虽然阅读方便了 但是对debug确造成一定的困扰&#xff0c;主要问题是line number的不match. Google了下遇到类似问题的真不少。最终找…

centos7安装最新版git_https://my.oschina.net/antsky/blog/514586

whoru 发表于 2年前 阅读 5961收藏 10点赞 1评论 1 腾讯云 普惠云计算 0门槛体验>>> 摘要: 介绍yum和源码包安装的具体方式 方式一、yum安装 # yum install git 通过yum方式安装&#xff0c;版本比较旧&#xff0c;CentOS6.5上安装好是1.7.1版。如果想安装最新版或…

SQLServer2008如何copydatabase--https://www.2cto.com/database/201411/351603.html

xjh测试有效 首页>数据库 > SQL Server > 正文SQLServer2008如何copydatabase2014-11-12 09:22:27 个评论 来源&#xff1a;tianshijianbing1989的专栏 收藏 我要投稿SQL Server 2008如何copy database&#xff1a; 方法一&#xff1a;使用Microsoft SQL Ser…

node.js的koa@2性能测试

一直都知道node.js的性能很好&#xff0c;但是却没有实际测试过&#xff0c;今天用ab实际测试一下。 先上结果&#xff1a;支持13000并发&#xff08;helloword&#xff09; CPU: inter 酷睿 i3-2330M 2.2GHz 4核 内存: 6G 系统: Win64 &#xff08;其实就是老旧的加装了4G内存…