ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制。

如果容器中存在ApplicationListener的Bean,当ApplicationContext调用publishEvent方法时,对应的Bean会被触发。这一过程是典型的观察者模式的实现。

ApplicationListener源码

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { /**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event); }

ContextRefreshedEvent事件的监听

以Spring的内置事件ContextRefreshedEvent为例,当ApplicationContext被初始化或刷新时,会触发ContextRefreshedEvent事件,

实现一个ApplicationListener来监听此事件的发生:

@Component // 需对该类进行Bean的实例化
public class LearnListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// 打印容器中出事Bean的数量
System.out.println("监听器获得容器中初始化Bean数量:" + event.getApplicationContext().getBeanDefinitionCount());
}
}

如上,便完成了一个事件监听类的实现和实例化。

自定义事件及监听

首先自定义事件:NotifyEvent。

public class NotifyEvent extends ApplicationEvent {

    private String email;

    private String content;

    public NotifyEvent(Object source) {
super(source);
} public NotifyEvent(Object source, String email, String content) {
super(source);
this.email = email;
this.content = content;
}
// 省略getter/setter方法
}

定义监听器NotifyListener:

@Component
public class NotifyListener implements ApplicationListener<NotifyEvent> { @Override
public void onApplicationEvent(NotifyEvent event) {
System.out.println("邮件地址:" + event.getEmail());
System.out.println("邮件内容:" + event.getContent());
}
}

监听器通过@Component注解进行实例化,并在onApplicationEvent中打印相关信息。

单元测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ListenerTest { @Autowired
private WebApplicationContext webApplicationContext; @Test
public void testListener() { NotifyEvent event = new NotifyEvent("object", "abc@qq.com", "This is the content"); webApplicationContext.publishEvent(event);
}
}

Spring中的ApplicationListener扩展使用的更多相关文章

  1. 详解Spring中的ApplicationListener和ContextRefreshedEvent

    ApplicationListener和ContextRefreshedEvent一般都是成对出现的.最近在面试中问到了被面试者对于这两个的用法,面试者大多数被问懵了.可见基础知识的掌握程度.基于此本 ...

  2. Spring中的ApplicationListener的使用详解案例

    本文链接:https://blog.csdn.net/u010963948/article/details/83507185 1.ApplicationContext Spring的核心,Contex ...

  3. spring中的ApplicationListener监听器

    监听器在使用过程中可以监听到某一事件的发生,进而对事件做出相应的处理. 首先自定义一个监听器myListener实现ApplicationListener接口 @Repository public c ...

  4. Spring中自定义Schema扩展机制

    一.前言 Spring 为基于 XML 构建的应用提供了一种扩展机制,用于定义和配置 Bean. 它允许使用者编写自定义的 XML bean 解析器,并将解析器本身以及最终定义的 Bean 集成到 S ...

  5. Spring中Aop的扩展及剖析

    AOP简介: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范 ...

  6. Spring中的可扩展接口

    1.监听器Listener(点此连接,执行流程带源码分析及demo) 2.bean定义的后置处理器(BeanDefinitionRegistryPostProcessor)和bean工厂的后置处理器( ...

  7. 三种方式实现观察者模式 及 Spring中的事件编程模型

    观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...

  8. Spring IoC 容器的扩展

    前言 本篇文章主要介绍 Spring 中 BeanFactory 的扩展 ApplicationContext,我们平时日常开发中也基本上是使用它,不会去直接使用 BeanFactory. 那么在 S ...

  9. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

随机推荐

  1. winform(C#)里弹出“确定”“取消”对话框

    //消息框中需要显示哪些按钮,此处显示“确定”和“取消” MessageBoxButtons messButton = MessageBoxButtons.OKCancel; //"确定要退 ...

  2. pytorch更新

    Pytorch如何更新版本与卸载,使用pip,conda更新卸载Pytorch 2018年05月22日 07:33:52 醉雨轩Y 阅读数 19047   今天我们主要汇总如何使用使用ubuntu,C ...

  3. 权值线段树+动态开点[NOI2004]郁闷的出纳员

    #include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> ...

  4. html--图片img

    一.图片的基本格式 当前万维网上流行的图像格式以GIF及JPEG为主,另外还有一个PNG.以下做分别介绍: 1.GIF格式:采用LZW压缩,是以压缩相同颜色的色块来减少图像大小的.(LZW压缩是一种能 ...

  5. js切割字符串

    var time_str= '2019-9-10 13:18:20'; var t = time_str.substr(2,8);   console.log(t);   输出  19-9-10

  6. C#的循环语句(一)

    循环:反复执行某段代码. 循环四要素:初始条件,循环条件,循环体,状态改变.for(初始条件;循环条件;状态改变) {循环体} for 格式: for(int i=1/*初始条件*/;0<=10 ...

  7. H3C PAP验证配置示例

  8. 指针版的PStash(用一个void指针数组, 来保存存入元素的地址) 附模板化实现 p321

    由容器PStash的使用者,负责清除容器中的所有指针.所以用户必须记住放到容器中的是什么类型,在取出时,把取出的void指针转换成对应的类型指针,然后 'delete 转换后的对象指针',才能在清除时 ...

  9. Hibernate @OneToOne懒加载实现解决方案

    在hibernate注解(三)中,我提高过一对一(@OneToOne)懒加载失效的问题.虽然给出了解决方法,但并没有给出完整的解决方案.今天我专门针对该问题进行讨论.至于懒加载失效的原因,在之前的文章 ...

  10. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)

    题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...