Spring中的ApplicationListener扩展使用
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扩展使用的更多相关文章
- 详解Spring中的ApplicationListener和ContextRefreshedEvent
ApplicationListener和ContextRefreshedEvent一般都是成对出现的.最近在面试中问到了被面试者对于这两个的用法,面试者大多数被问懵了.可见基础知识的掌握程度.基于此本 ...
- Spring中的ApplicationListener的使用详解案例
本文链接:https://blog.csdn.net/u010963948/article/details/83507185 1.ApplicationContext Spring的核心,Contex ...
- spring中的ApplicationListener监听器
监听器在使用过程中可以监听到某一事件的发生,进而对事件做出相应的处理. 首先自定义一个监听器myListener实现ApplicationListener接口 @Repository public c ...
- Spring中自定义Schema扩展机制
一.前言 Spring 为基于 XML 构建的应用提供了一种扩展机制,用于定义和配置 Bean. 它允许使用者编写自定义的 XML bean 解析器,并将解析器本身以及最终定义的 Bean 集成到 S ...
- Spring中Aop的扩展及剖析
AOP简介: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范 ...
- Spring中的可扩展接口
1.监听器Listener(点此连接,执行流程带源码分析及demo) 2.bean定义的后置处理器(BeanDefinitionRegistryPostProcessor)和bean工厂的后置处理器( ...
- 三种方式实现观察者模式 及 Spring中的事件编程模型
观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...
- Spring IoC 容器的扩展
前言 本篇文章主要介绍 Spring 中 BeanFactory 的扩展 ApplicationContext,我们平时日常开发中也基本上是使用它,不会去直接使用 BeanFactory. 那么在 S ...
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
随机推荐
- Linux下的一些配置
/etc/vim/vimrc文件 set cindent set expandtab set smartindent set autoindent set nu set hls taglist安装(t ...
- C++第三次作业:友元类
友元类 将数据与处理数据的函数封装在一起,构成类,即实现了数据的共享又实现了隐藏,无疑是面向程序设计的一大优点,但是封装并不总是完美的,一旦需要涉及到一个类的两个对象的数据处理问题该怎么办?无论是设计 ...
- tp5 select出来数据集(对象)转成数组
1.先在数据库配置文件中 //数据集返回类型 'resultset_type' => 'collection', 2.在使用时, 使用 toArray() 方法 //查询数据库 $news = ...
- 【u227】BOOK
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 陈老师喜欢网购书籍,经常一次购它个百八十本,然后拿来倒卖,牟取暴利.前些天,高一的新同学来了,他便像往 ...
- 2018-8-10-win10-uwp-MVVM-语义耦合
title author date CreateTime categories win10 uwp MVVM 语义耦合 lindexi 2018-08-10 19:16:53 +0800 2018-2 ...
- Linux 内核 ksets 之上的操作
对于初始化和设置, ksets 有一个接口非常类似于 kobjects. 下列函数存在: void kset_init(struct kset *kset); int kset_add(struct ...
- geoip ip2region2 with spark
上一篇文章中 我使用 maxmind的免费库开发了一个waterdrop的 插件,测试数据发现,国内的有些市级还是不准确,而且香港并不是显示中国,这就不友好了. 找了一下,发下 ip2region 这 ...
- 使用SuperWebSocket实现Web消息推送
在大部分Web系统中,我们可能遇到需要向客户端推送消息的需求.SuperWebSocket第三方库能让我们轻松的完成任务.SuperWebSocket第三方库可以从网上下载,不过通过Visual St ...
- 驳“C语言已经死了”
http://blog.csdn.net/xushiweizh/article/details/1476422
- Object 与 Function那神奇而混乱的搞基关系
// Object 与 Function神奇而混乱的搞基关系... Object.__proto__ === Function.prototype; // true Object.__proto__ ...