什么是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. FZU 2020 组合

    组合数求模要用逆元,用到了扩展的欧几里得算法. #include<cstdio> int mod; typedef long long LL; void gcd(LL a,LL b,LL ...

  2. go 生成随机小数 指定范围

    package main import ( "crypto/hmac" "crypto/sha1" "encoding/base64" &q ...

  3. SetForegroundWindow

    SetForegroundWindow 函数功能:该函数将创建指定窗口的线程设置到前台,并且激活该窗口.键盘输入转向该窗口,并为用户改各种可视的记号.系统给创建前台窗口的线程分配的权限稍高于其他线程. ...

  4. HASH算法具体解释

    做了几年开发,一直不理解HASH算法的原理.今天偶从百度知道上看到一个牛人神一样的理解: 这个问题有点难度.不是非常好说清楚. 我来做一个比喻吧. 我们有非常多的小猪,每一个的体重都不一样,假设体重分 ...

  5. LUA凝视语法

    server端代码已经完毕,client正在优化.游戏不久将上线,近期没事做,老大要我開始学习project Anarchy了.里面代码是比較偏爱的C++,包括lua,暂没学过lua.看了下LUA代码 ...

  6. js导出报表

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78376156 需求:项目中有一个学生签到模块需要导出每天的签到数据,一开始用poi在后 ...

  7. [Java][web]利用Spring随时随地获得Request和Session

    利用Spring随时随地获得Request和Session 一.准备工作: 在web.xml中加入 <listener> <listener-class> org.spring ...

  8. Android MagicIndicator系列之一 —— 使用MagicIndicator打造千变万化的ViewPager指示器

    说到 ViewPager 指示器,想必大家都不陌生,绝大部分应用中都有这个.使用频率非常之高.但系统对它的支持并不好,自带的 PagerTabStrip 和 PagerTitleStrip 太弱,很难 ...

  9. 通过select下拉框里的value控制div显示与隐藏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. .Net Core Socket 压力测试

    原文:.Net Core Socket 压力测试 .Net Core Socket 压力测试 想起之前同事说go lang写的push service单机可以到达80万连接,于是就想测试下.Net C ...