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 ...
随机推荐
- js 文件上传
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- GitHub 中国区前 100 名到底是什么样的人?
转一下CSDN的文章, 这里有些人挺厉害的. http://geek.csdn.net/news/detail/66000
- Linux下搜索文件
使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为"find":"locate":"whereis& ...
- 使用Maven + Jetty时,如何不锁定js css 静态资源
Jetty会使用内存映射文件来缓存静态文件,包括js,css文件. 在Windows下,使用内存映射文件会导致文件被锁定,所以当Jetty启动的时候无法在编辑器对js或者css文件进行编辑. 解决办法 ...
- 一些有趣的使用function
转载来源:新人必看的短小而精悍的javascript function 1.回到顶部,优点使用浏览器刷新频率的requestAnimationFrame,很顺滑 const scrollToTop = ...
- Android 时钟(由秒转变为时分秒)
int second = 0: Handler handler = new Handler(); handler.post(runnable); Runnable runnable = new Run ...
- dns 服务架构优化 - 百万级并发不是梦 - bind+namedmanager+dnsmasq
bind: DNS服务端. namedmanager: DNS web管理页面. dnsmasq: 并发查询上游dns域名解析. 问题:作为消息推送业务,单台业务机器域名解析并发达到上万次.业务机器集 ...
- Dos命令下目录操作
Dos命令下目录操作 1.cd 操作 显示当前目录名或改变当前目录 cd [盘符][路径] 进入指定盘符下的目录 cd [..] ...
- 给Ajax一个漂亮的嫁衣——Ajax系列之五(下)之序列化和反序列化
给Ajax一个漂亮的嫁衣——Ajax系列之五(下)之序列化和反序列化 标签: ajaxdictionaryjsonobject服务器function 2012-07-25 18:41 2242人阅读 ...
- W1002 Symbol 'Create' is specific to a platform
http://stackoverflow.com/questions/9099892/how-to-use-tformatsettings-create-without-being-specific- ...