spring之ApplicationEvent 事件驱动
什么是ApplicationContext?
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEvent
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Observer设计模式
首先创建一个Event事件类
public class EmailListEvent extends ApplicationEvent {
2.
3. private static final long serialVersionUID = 1L;
4. public String address;
5. public String text;
6.
7. public EmailListEvent(Object source) {
8. super(source);
9. }
10.
11. public EmailListEvent(Object source, String address, String text) {
12. super(source);
13. this.address = address;
14. this.text = text;
15. }
16.
17. public void print() {
18. System.out.println("Hello,Spring Event!!!");
19. }
20. }
其次创建一个ApplicationListener类:
public class EmailNotifier implements ApplicationListener {
2.
3. public void onApplicationEvent(ApplicationEvent evt) {
4. if (evt instanceof EmailListEvent) {
5. EmailListEvent emailEvent = (EmailListEvent) evt;
6. emailEvent.print();
7. System.out.println("the source is:" + emailEvent.getSource());
8. System.out.println("the address is:" + emailEvent.address);
9. System.out.println("the mail's context is :" + emailEvent.text);
10. }
11.
12. }
接着将Listener注册到Spring的xml文件中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="emailListListener" class="spring.event.EmailNotifier"></bean> </beans>
MAIN
1. public class ListenerEventDemo {
2.
3. /**
4. * @param args
5. */
6. public static void main(String[] args) {
7. ApplicationContext context = new ClassPathXmlApplicationContext(
8. "SpringEvent.xml");
9. EmailListEvent emailListEvent = new EmailListEvent("hello",
10. "helloSpring@sina.com", "this is a test eamil content");
11. //在ApplicationContext中发布一个 ApplicationEvent
12. context.publishEvent(emailListEvent);
13. }
14.
测试结果:
# Hello,Spring Event!!!
# the source is:hello
# the address is:helloSpring@sina.com
# the mail's context is :this is a test eamil content
spring之ApplicationEvent 事件驱动的更多相关文章
- Spring中ApplicationEvent和ApplicationListener封装
1.测试程序EventTest.java,发布一个事件只需要调用FrameEventHolder.publishEvent()方法即可. package com.junge.spring.event; ...
- [案例一] Spring中的事件驱动模型(机制)
事件驱动模型是观察者模式的另一种形态,观察者相当于监听器,被观察者相当于事件源 事件源产生事件,监听器监听事件 以用户注册时候,要发送邮件和发送短信举例说明 定义一个事件 /** * spring会自 ...
- Spring事件,ApplicationEvent在业务中的应用
前言 关于事件驱动模型,百度百科在有明确的解释.在JDK的Util包里抽象了事件驱动,有兴趣的朋友可以自行去看下相关类的定义.Spring事件模型ApplicationEvent是基于JDK里的事件模 ...
- Spring的ApplicationEvent实现
原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播.通过ApplicationCont ...
- Spring 3.2 事件驱动模型
事件 @SuppressWarnings("serial") public class CheckEvent extends ApplicationEvent { public C ...
- Spring 的 ApplicationEvent and ApplicationListener
什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,可是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...
- 利用Spring的ApplicationEvent执行自定义方法
在Spring中已经定义了五个标准事件,分别介绍如下: 1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件. 2)ContextClose ...
- 详解Spring事件驱动模型
转载自:http://jinnianshilongnian.iteye.com/blog/1902886#comments 事件驱动模型简介 事件驱动模型也就是我们常说的观察者,或者发布-订阅模型:理 ...
- spring事件驱动模型--观察者模式在spring中的应用
spring中的事件驱动模型也叫作发布订阅模式,是观察者模式的一个典型的应用,关于观察者模式在之前的博文中总结过,http://www.cnblogs.com/fingerboy/p/5468994. ...
随机推荐
- Cys_Control(一) 项目搭建
一.基础工程搭建 Cys_Controls Cys_Resource(注:一般类库默认不能引入资源文件,故直接创建Custom Control Library) Cys_Demo 删除默认文件夹及类, ...
- moviepy音视频剪辑:视频半自动追踪人脸打马赛克
一.引言 在<moviepy1.03音视频剪辑:使用manual_tracking和headblur实现追踪人脸打马赛克>介绍了使用手动跟踪跟踪人脸移动轨迹和使用headblur对人脸进行 ...
- 第8.7节 Python类__new__方法和构造方法关系深入剖析:__new__方法执行结果对__init__的影响案例详解
一. 引言 前面章节介绍了类中的构造方法和__new__方法,并分析了二者执行的先后顺序关系.__new__方法在__init__方法前执行,__new__方法执行后才返回实例对象,也就是说__new ...
- 第11.7节 Python正则表达式的字符串结尾匹配模式及元字符“$”功能介绍
符号"$"表示匹配字符串的结尾,即字符串的结尾满足匹配模式的要求. 在 MULTILINE 模式(搜索标记中包含re.MULTILINE,关于搜索标记的含义请见<第11.2节 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中QAbstractButton派生按钮部件autoRepeat、autoRepeatDelay、autoRepeatInterval属性
autoRepeat.autoRepeatDelay.autoRepeatInterval这三个属性为一组属性,用于控制按钮的按下事件是否重复.重复的频次等. autoRepeat 如果启用了auto ...
- NET CORE通过NodeService调用js
在 .NET Framework 时,我们可以通过V8.NET等组件来运行 JavaScript,不过目前我看了好几个开源组件包括V8.NET都还不支持 .NET Core ,我们如何在 .NET C ...
- 6种css3 transform图片悬停动态效果
html骨架代码 <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...
- Codeforces Edu Round 49 A-E
A. Palindromic Twist 由于必须改变.所以要使\(a[i] = a[n - i + 1]\). 要么同向走,但必须满足之前的\(a[i] = a[n - i + 1]\). 要么相遇 ...
- 快用Django REST framework写写API吧
Django默认是前后端绑定的,提供了Template和Form,现在流行前后端分离项目,Python大佬坐不住了,于是便有了Django REST framework:https://github. ...
- day111:MoFang:邀请好友流程&生成邀请好友二维码&第三方应用识别二维码&本地编译测试&记录邀请人信息
目录 1.邀请业务逻辑流程图 2.邀请好友-前端 3.邀请好友-后端接口(生成二维码) 4.前端获取后端生成的二维码 5.前端长按页面,保存图片到相册 6.客户端通过第三方识别微信二维码,服务端提供对 ...