一: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的事件发布机制的更多相关文章

  1. 【spring源码学习】spring的事件发布监听机制源码解析

    [一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...

  2. 从spring源码汲取营养:模仿spring事件发布机制,解耦业务代码

    前言 最近在项目中做了一项优化,对业务代码进行解耦.我们部门做的是警用系统,通俗的说,可理解为110报警.一条警情,会先后经过接警员.处警调度员.一线警员,警情是需要记录每一步的日志,是要可追溯的,比 ...

  3. Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制

    上一篇文章https://www.cnblogs.com/redwinter/p/16198942.html介绍了Spring的注解的解析过程以及Spring Boot自动装配的原理,大概回顾下:Sp ...

  4. Nacos源码分析-事件发布机制

    温馨提示: 本文内容基于个人学习Nacos 2.0.1版本代码总结而来,因个人理解差异,不保证完全正确.如有理解错误之处欢迎各位拍砖指正,相互学习:转载请注明出处. Nacos的服务注册.服务变更等功 ...

  5. spring 自定义事件发布及监听(简单实例)

    前言: Spring的AppilcaitionContext能够发布事件和注册相对应的事件监听器,因此,它有一套完整的事件发布和监听机制. 流程分析: 在一个完整的事件体系中,除了事件和监听器以外,还 ...

  6. Spring之事件发布系统

    springboot应用,启动spring容器大致有如下几个过程: 容器开始启动 初始化环境变量 初始化上下文 加载上下文 完成 对应的Spring应用的启动器的监听器可以监听以上的过程,接口如下: ...

  7. spring boot 事件发布与接收

    1.创建发布对象 LoginEvent 2.在要发布对象的地方注入 ApplicationEventPublisher @Autowired ApplicationEventPublisher pub ...

  8. Spring的事件监听机制

    最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...

  9. spring事件通知机制详解

    优势 解耦 对同一种事件有多种处理方式 不干扰主线(main line) 起源 要讲spring的事件通知机制,就要先了解一下spring中的这些接口和抽象类: ApplicationEventPub ...

随机推荐

  1. Codeforces 556D - Case of Fugitive

    556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/s ...

  2. 工程优化暨babel升级小记

    小记背景 随着业务代码的增多,项目代码的编译时长也在增多,遂针对这个痛点在dev下做些优化 第一部分:优化dev编译时间 这里优化的主要思路是在dev环境下,单独出来一个dll配置文件,将项目中的部分 ...

  3. 顶点与UV

    1.顶点坐标和UV坐标是三维模型重要的两个坐标系统. 2.什么是UV?UV分别是图像在显示器水平和垂直方向上坐标,值在 0 - 1 之间 ,即水平方向的第 U 个做像素/图片宽度,垂直方向的第 V 个 ...

  4. c# 获取方法所在的命名空间 类名 方法名

    平时我们在记录日志的时候难免会需要直接记录当前方法的路径,以便查找,但是每次都输入方法名称非常的繁琐,同时如果修改了方法名称也要去手动修改日志内容,真的是劳命伤财啊,所以有了如下方法则可解决我们的大难 ...

  5. English trip -- VC(情景课)2 D Reading

    Xu言: 业精于勤,荒于嬉:行成于思,毁于随 Before you read 阅读准备 Talk about the picture, what do you see?看图说话,你看到了什么? Lis ...

  6. Cron\CronExpression::setPart("24")

    利用laravle实现定时器的功能的时候,报错说:Cron\CronExpression::setPart("24"). 后来发现是时间设置的问题,他不能设置("24:0 ...

  7. tensorflow安装相关问题

    安装步骤 要求:Python必须是64位根据TensorFlow的计算方式,TensorFlow的安装分为CPU版本和GPU版本对于Python3.5或者Python3.6,可以使用pip insta ...

  8. kill prefix extra,endo out 1

      1●extra 超过外面的, 以外的,外面 的   2●endo   内部  

  9. POJ 2752 KMP中next数组的理解

    感觉这里讲的挺好的.http://cavenkaka.iteye.com/blog/1569062 就是不断递归next数组.长度不断减小. 题意:给你一个串,如果这个串存在一个长度为n的前缀串,和长 ...

  10. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...