Spring的事件发布机制
一:Spring的事件发布
ApplicationContext提供了针对Bean的事件传播功能,其中的主角是publishEvent()方法,通过这个方法可以将事件通知给系统内的监听器(需实现ApplicationListener接口)。
ApplicationContext这个接口,是Spring的上下文,通常获取Bean就需要这个接口,这个接口并不是直接继承于BeanFactory,其中最著名的是直接继承了ApplicationPublisher接口,这个接口查看源码可以发现:只有一个方法,那就是主角 void publishEvent(ApplicationEvent event);
Spring提供的基于Aware相关的接口有ApplicationContextAware,ResourceloaderAware,ServletContextAware(注意:Struts2也有这个接口,注意区分),最常用的就这三个,而Spring的事件发布机制需要用到ApplicationContextAware接口。
实现了ApplicationContextAware的Bean,在Bean初始化时将会被注入ApplicationContext实例(因为这个接口里有set(ApplictationContext ctx)方法)
二:有了以上基础,看示例代码:
1.首先创建事件类 TradeEvent
package net.wang.test; import org.springframework.context.ApplicationEvent; /**
* 事件Event
* @author LiuRuoWang
*/
public class TradeEvent extends ApplicationEvent{ public TradeEvent(Object source) {
super(source);
System.out.println("事件:TradeEvent event !!");
} }
事件必须继承Spring提供的ApplicationEvent抽象类
2.然后编写 事件的发布者HelloWorld:
package net.wang.test; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; /**
* 事件的发布者
* @author LiuRuoWang
*/
public class HelloWorld implements ApplicationEventPublisherAware{ private String word; public void setWord(String word) {
this.word = word;
} private ApplicationEventPublisher tradeEventPublisher; public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.tradeEventPublisher=applicationEventPublisher;
} public void say(){
System.out.println("say:"+this.word);
TradeEvent tradeEvent = new TradeEvent(new String("HelloWorld!"));
this.tradeEventPublisher.publishEvent(tradeEvent);
} }
其中在say()方法里发布了事件
3.最后编写 事件的接收者EventReceiver:
package net.wang.test; import org.springframework.context.ApplicationListener; /**
* 事件的接收者
* @author LiuRuoWang
*/
public class EventReceiver implements ApplicationListener<TradeEvent>{ public void onApplicationEvent(TradeEvent event) {
System.out.println("监听到的事件:"+event.getSource());
}
}
事件的接收者其实是一个监听器,必须实现ApplicationListener,注意把事件TradeEvent直接写到泛型中
4.applicationContext.xml:
<?xml version="1.0" encoding="GBK"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <bean name="helloWrold" class="net.wang.test.HelloWorld">
<property name="word" value="Word!"/>
</bean> <bean name="eventReceiver" class="net.wang.test.EventReceiver"/> </beans>
注意把事件的接收者写入配置文件中
5.测试Test:
package net.wang.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld h = (HelloWorld) ctx.getBean("helloWrold");
h.say();
}
}
6.结果显示:

结果中已经显示监听到的事件,说明成功。
Spring的事件发布机制的更多相关文章
- 【spring源码学习】spring的事件发布监听机制源码解析
[一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...
- 从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码
前言 最近在项目中做了一项优化,对业务代码进行解耦.我们部门做的是警用系统,通俗的说,可理解为110报警.一条警情,会先后经过接警员.处警调度员.一线警员,警情是需要记录每一步的日志,是要可追溯的,比 ...
- Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制
上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...
- Nacos源码分析-事件发布机制
温馨提示: 本文内容基于个人学习Nacos 2.0.1版本代码总结而来,因个人理解差异,不保证完全正确.如有理解错误之处欢迎各位拍砖指正,相互学习:转载请注明出处. Nacos的服务注册.服务变更等功 ...
- spring 自定义事件发布及监听(简单实例)
前言: Spring的AppilcaitionContext能够发布事件和注册相对应的事件监听器,因此,它有一套完整的事件发布和监听机制. 流程分析: 在一个完整的事件体系中,除了事件和监听器以外,还 ...
- Spring之事件发布系统
springboot应用,启动spring容器大致有如下几个过程: 容器开始启动 初始化环境变量 初始化上下文 加载上下文 完成 对应的Spring应用的启动器的监听器可以监听以上的过程,接口如下: ...
- spring boot 事件发布与接收
1.创建发布对象 LoginEvent 2.在要发布对象的地方注入 ApplicationEventPublisher @Autowired ApplicationEventPublisher pub ...
- Spring的事件监听机制
最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...
- spring事件通知机制详解
优势 解耦 对同一种事件有多种处理方式 不干扰主线(main line) 起源 要讲spring的事件通知机制,就要先了解一下spring中的这些接口和抽象类: ApplicationEventPub ...
随机推荐
- idea忽略文件
- Codeforces 898F - Restoring the Expression(字符串hash)
898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...
- [Java学习] Java super关键字
super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类. super 可以用在子类中,通过点号(.)来获取父类的成员变量和方法.super 也可以用在子类的子类 ...
- English trip -- 国际音标表
26个字母音标表 A a [ei] B b [bi:] C c [si:] D d [di:] E e [i:] F f [ef] G g [dʒi:] H h [eit∫] I i [ai] J j ...
- Session问题-一个部门A登录后未注销另一个部门B再登录,以B的身份操作A的成员
当管理员A登录后,打开某个管理界面.在A和B权限菜单相同的情况下,管理员B新开页面登录,session变为B的,然后切换到A打开的界面,不刷新的情况下可以操作A的数据. 解决方案:当浏览器登录后,即存 ...
- Ivan and Burgers CodeForces - 1100F (线性基)
大意: 给定n元素序列, m个询问$(l,r)$, 求$[l,r]$中选出任意数异或后的最大值 线性基沙茶题, 直接线段树暴力维护两个log还是能过的 #include <iostream> ...
- Shell脚本中点号+文件名的作用
熟悉linux是程序员必须具备的技能,水滴石穿,一点点学习吧. linux中一个文件是根据其是否具有执行属性来判断他是否可以直接运行的.就像windows下的exe一样. 如果我们要执行某一个文件,可 ...
- mac 地址
- Charles安装破解、抓取https请求及常见问题解决方法
现在基本大部分网站都使用了https,所以要想抓到https的请求,首要任务是先有工具:charles.fiddler,先介绍下charles针对https请求的抓取方法,此方法兼容windows和m ...
- 现在转战c++的领域,纯幼儿园
C++中: 如果你用#include<iostream.h>就不需写这句话(旧标准).但是如果你用#include<iostream>就必须要写.但是在VS2010中就出现错误 ...