什么是ApplicationContext?


它是Spring的核心,Context我们通常解释为上下文环境,可是理解成容器会更好些。

ApplicationContext则是应用的容器。

Spring把Bean(object)放在容器中,须要用就通过get方法取出来。

ApplicationEvent

是个抽象类,里面仅仅有一个构造函数和一个长整型的timestamp。

ApplicationListener

是一个接口,里面仅仅有一个onApplicationEvent方法。

所以自己的类在实现该接口的时候,要实装该方法。

假设在上下文中部署一个实现了ApplicationListener接口的bean,

那么每当在一个ApplicationEvent公布到 ApplicationContext时,

这个bean得到通知。事实上这就是标准的Observer设计模式。

首先创建一个Event事件类:

   1. 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类:

   1. 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. }
13. }

接着将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>

最后创建Demo类:

   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.
15. }

測试结果:

# 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 and ApplicationListener的更多相关文章

  1. Spring中ApplicationEvent和ApplicationListener封装

    1.测试程序EventTest.java,发布一个事件只需要调用FrameEventHolder.publishEvent()方法即可. package com.junge.spring.event; ...

  2. Spring的ApplicationEvent实现

    原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播.通过ApplicationCont ...

  3. Spring事件监听ApplicationListener源码流程分析

    spring的事件机制是基于观察者设计模式的,ApplicationListener#onApplicationEvent(Event)方法,用于对事件的处理 .在容器初始化的时候执行注册到容器中的L ...

  4. spring之ApplicationEvent 事件驱动

    什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...

  5. Spring事件,ApplicationEvent在业务中的应用

    前言 关于事件驱动模型,百度百科在有明确的解释.在JDK的Util包里抽象了事件驱动,有兴趣的朋友可以自行去看下相关类的定义.Spring事件模型ApplicationEvent是基于JDK里的事件模 ...

  6. 利用Spring的ApplicationEvent执行自定义方法

    在Spring中已经定义了五个标准事件,分别介绍如下: 1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件. 2)ContextClose ...

  7. web服务启动spring自己主动运行ApplicationListener的使用方法

    我们知道.一般来说一个项目启动时须要载入或者运行一些特殊的任务来初始化系统.通常的做法就是用servlet去初始化.可是servlet在使用spring bean时不能直接注入,还须要在web.xml ...

  8. spring boot 之监听器ApplicationListener

    监听器ApplicationListener 就是spring的监听器,能够用来监听事件,典型的观察者模式.ApplicationListener和ContextRefreshedEvent一般都是成 ...

  9. Spring监听,ApplicationListener

    import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import ...

随机推荐

  1. 如何在Win8/Win10上开启 dotNetFramework 2.0/3.5 功能

    问题: 在Windows 8.Windows 10上安装一些软件时,系统可能会报出如下错误:你的电脑上的应用需要使用以下Windows功能:     解决方式:   首先呢,你需要准备好一个Win8/ ...

  2. 10.3、android输入系统_必备Linux编程知识_任意进程双向通信(scoketpair+binder)

    3. 任意进程间通信(socketpair_binder) 进程每执行一次open打开文件,都会在内核中有一个file结构体表示它: 对每一个进程在内核中都会有一个task_struct表示进程,这个 ...

  3. JRebel热部署神器的配置(Eclipse,非教程,就自己看看)

    1.安装下载 直接下官方正版的就好了 eclipse->help->eclipse marketplace 搜索JRebel 然后按步骤一步步安好 安装好记得重启 2.注册 这东西在搞活动 ...

  4. Html animation by css(Sequence Js Tutorial)

    In sequence js,the javascript make load css definitation and make animation. 1.Start state #sequence ...

  5. swift项目第十天:网络请求工具类的封装

    import UIKit /* 必须先导入头文件:import AFNetworking */ import AFNetworking //MARK:-0:定义枚举:以枚举定义请求网络的get和pos ...

  6. swift项目第九天:正则表达式的学习

    import UIKit /* 练习1:匹配abc 练习2:包含一个a~z,后面必须是0~9 -->[a-z][0-9]或者[a-z]\d * [a-z] : a~z * [0-9]/\d : ...

  7. C语言编程程序的内存怎样布局

    在c语言中,每一个变量和函数有两个属性:数据类型和数据的存储类别. C语言中局部变量和全局变量变量的存储类别(static,extern,auto,register) 1. 从变量的作用域划分变量(即 ...

  8. ImageButton按压效果失效

    LinearLayout中ImageButton的按压效果不起作用,如图 布局如下: <LinearLayout android:id="@id/ll_add_reply_face&q ...

  9. fastjson排序 Map多层嵌套转换自动排序问题终极解决方案

    阅读更多 最近项目中用到了fastjson(1.2.15)需要将前端多层嵌套json转换为map,由于map的无序性,想了很多办法,最终找到使用 Map m= JSONArray.parseObjec ...

  10. Linux下kill进程脚本

    Linux下kill进程脚本 在Linux有时会遇到需要kill同一个程序的进程,然而这个程序有多个进程,一一列举很是繁琐,使用按名字检索,统一kill Perl脚本 使用方法 kill_all.pl ...