什么是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的使用

    Spring的ApplicationEvent的使用 Spring 3.0中提供了很多类似*Aware的类,其中ApplicationContextAware接口可以实现我们在初始化bean的时候给b ...

  2. SpringBoot:ApplicationEvent与ApplicationListener

    1.美图 2.概述 Spring的世界中有这么一对好基友,两个人总是同时出现,相爱相杀.这就是ApplicationEvent与ApplicationListener.感觉有点像小偷和警察,只要App ...

  3. ApplicationEvent与ApplicationListener

    前言: Spring的世界中有这么一对好基友,两个人总是同时出现,相爱相杀.这就是ApplicationEvent与ApplicationListener.感觉有点像小偷和警察,只要Applicati ...

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

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

  5. springboot中使用ApplicationListener和ApplicationEvent /@EventListener监听事件

    文章目录 自定义一个事件类 监听类: 事件发布者 改用@EventListener注解: 总结 自定义一个事件类 public class OnRegistrationCompleteEvent ex ...

  6. 框架源码专题:Spring的事件监听、发布机制 ApplicationListener

    文章目录 1.Spring内置事件 2.自定义事件 3.事件监听器 4.事件发布 publishEvent 4.Spring事件原理 5. 面试题:怎么样可以在所有Bean创建完后做扩展代码? 6. ...

  7. 七.Spring之ApplicationListener事件监听、@EventListener

    看看注释:由应用事件监听器实现的接口,基于观察者设计模式. 方法是处理应用事件. /**由应用事件监听器实现的接口,基于观察者设计模式* Interface to be implemented by ...

  8. Spring Boot————Web应用启动时自动执行ApplicationListener用法

    原文:<web服务启动spring自动执行ApplicationListener的用法> 引言 我们知道,一般来说一个项目启动时需要加载或者执行一些特殊的任务来初始化系统,通常的做法就是用 ...

  9. Spring笔记——2.使用Spring容器

    Spring有两个核心接口:BeanFactory和ApplicationContext.而ApplicationContext是BeanFactory的子接口,都可以代表Spring容器,用于产生管 ...

  10. Spring中ApplicationContext和beanfactory区别

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

最新文章

  1. 薛澜:人工智能发展要让创新驱动和敏捷治理并驾齐驱
  2. emeditor利用书签功能导出匹配结果到新文件
  3. 1.6 this关键字详解(3种用法)
  4. unity安装,sdk,jdk问题
  5. boost::fusion::unfused_typed用法的测试程序
  6. alibaba 实体转json_com.alibaba.fastjson 转换List Map等对象
  7. 使用 jQuery Mobile 与 HTML5 开发 Web App (十九) —— HTML5 对 Web App 的影响
  8. 太晚了!iPhone屏下指纹或要等到2021年才安排
  9. swing获取文本框内容_营销管理培训课件,大客服销售策略和技巧,全内容PPT拿来就用...
  10. c语言中字母后面的 是什么意思,C语言中括号里面一个字母是什么意思
  11. 企业研发效能月刊:告别996,走向“211”!
  12. ubuntu搭建无盘服务器,ubuntu配置无盘服务器
  13. Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(2) 自己封装的Calendar接口
  14. 苹果保修期查询_保修期还有6个月的IPhone X 换个“新”主板变“过保”了 维修点这样回应...
  15. vue如何判断iOS与Android系统
  16. C#基于联通短信Sgip协议构建短信网关程序.net
  17. 忙忙碌碌不过这碎银几两
  18. 什么是Pythonic?
  19. OMAP3630 Linux I2C总线驱动分析(2)
  20. 重磅榜单!互联网金融Top100总估值超1.1万亿,27家独角兽上榜!

热门文章

  1. brave mysql_MYSQL常用命令
  2. 南加大计算机硕士学制,2020年南加州大学硕士读几年
  3. JavaWeb 基础系列篇
  4. 我用一篇文章,让你快速上手Kotlin
  5. SQL Server-【知识与实战I】条件查询、比较运算符查询、模糊查询、枚举查询、范围查询、空值查询、多重条件查询
  6. JDBC — JDBC之Insert方法
  7. 7-1 Programming in C is fun! (5 分)
  8. roseMirrorHA5.0 for WindowsServer2008R2配合sqlserver2012|Oracle 11g的安装和配置
  9. Numpy包简单介绍
  10. position:fixed 失效