一: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.结果显示:

 

结果中已经显示监听到的事件,说明成功。

最新文章

  1. 第四章 springboot + swagger
  2. Sublime Text 使用笔记
  3. spring缓存Ehcache(入门2)
  4. php中rsa加密及解密和签名及验签
  5. Android侧滑
  6. IIS网站部署注意点
  7. UIKit-3347.44.2/UICollectionView.m:3443
  8. Amazon教程:刚买就降价!避免损失,申请PRICE MATCH(价格保护)的方法
  9. 【转】iOS-Core-Animation-Advanced-Techniques(四)
  10. English--Computer System
  11. ElasticSearch 学习记录之ES几种常见的聚合操作
  12. 《深入java虚拟机》读书笔记之垃圾收集器与内存分配策略
  13. CSS3_元素拖曳原理_设置全局点击捕获_九宫格碰撞检测_自定义滚动条
  14. 手推C3算法
  15. Python网络编程之Socket的简单实现
  16. 一文看懂npm、yarn、pnpm之间的区别
  17. 嵌入式LINUX设置时间
  18. (转)java基础-反射
  19. python安装pymssql
  20. C#.net实现图片上传功能

热门文章

  1. chrome表单自动填充去掉input黄色背景
  2. 让flask在出现语法错误时仍然自动重启
  3. VSAN Cluster Failed
  4. U3D协程yield的使用和理解
  5. 百度echart如何动态生成图表
  6. 雷林鹏分享:C# 正则表达式
  7. 快速读入fread
  8. JDBC连接SQLSERVER
  9. 使用路径arc-奥运五环
  10. OC 方法和函数