监听器在使用过程中可以监听到某一事件的发生,进而对事件做出相应的处理。

首先自定义一个监听器myListener实现ApplicationListener接口

@Repository
public class myListener implements ApplicationListener<ApplicationEvent>{
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("监听到的事件发布。。。。。。。。。。"+event.getClass());
System.out.println("监听的内容。。。。。。。。。。"+event.toString());
}
}

创建配置类MainListenerConfig:将myListener组件加入到容器中

@Configuration
@Import(myListener.class)
public class MainListenerConfig { }

测试

  public class ListenerTest {

    @Test
public void test(){
//创建容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainListenerConfig.class);
applicationContext.publishEvent(new ApplicationEvent("我发布的事件") {});
applicationContext.close();
}
}

打印输出:可以监听到自己发布的事件和spring容器在创建实例化销毁的过程中的发布事件。

spring中的ApplicationListener监听器的更多相关文章

  1. Spring中的ApplicationListener扩展使用

    ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...

  2. 详解Spring中的ApplicationListener和ContextRefreshedEvent

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

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

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

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

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

  5. Spring中ApplicationListener的使用

    背景 ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Appl ...

  6. 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题

    1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...

  7. Spring中ApplicationContext对事件的支持

    Spring中ApplicationContext对事件的支持   ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...

  8. 如何使用spring中的Log4jConfigListener--删除

    使用spring中的Log4jConfigListener有如如下好处:    1. 动态的改变记录级别和策略,不需要重启Web应用,如<Effective Enterprise Java> ...

  9. spring事件驱动模型--观察者模式在spring中的应用

    spring中的事件驱动模型也叫作发布订阅模式,是观察者模式的一个典型的应用,关于观察者模式在之前的博文中总结过,http://www.cnblogs.com/fingerboy/p/5468994. ...

随机推荐

  1. C# 判断一个string型的时间格式是否正确

    在项目开发过程中,由于各种坑爹的需求,我们可能需要用户自己手动输入时间,不过这种功能一般都出现在自己家的后台里面,咳咳,言归正传.既然如此,那么这个时候我们就需要对用户手动输入的时间格式进行验证,方法 ...

  2. [Algorithm] Tree Width with Level Width

    // --- Directions // Given the root node of a tree, return // an array where each element is the wid ...

  3. 在多语句事务内不允许使用 CREATE DATABASE 语句。

    方法一:create database [ 项目名称] 方法二:update-database -verbose

  4. csv和xlsx区别

    CSV是文本文件,用记事本就能打开.XLS 是二进制的文件只有用 EXCEL 才能打开: CSV 文件格式只能保存活动工作表中的单元格所显示的文本和数值.数据列以逗号分隔,每一行数据都以回车符结束.如 ...

  5. box-orient

    box-orient 语法: box-orient:horizontal | vertical | inline-axis | block-axis 默认值:horizontal 适用于:伸缩盒容器大 ...

  6. mousemove([[data],fn])

    mousemove([[data],fn]) 概述 当鼠标指针在指定的元素中移动时,就会发生 mousemove 事件.大理石构件来图加工 mousemove事件处理函数会被传递一个变量——事件对象, ...

  7. 路由器配置——基于链路的OSPF的MD5口令认证

    一.实验目的:掌握基于链路的OSPFMD5口令认证 二.拓扑图: 三.具体步骤配置: (1)R1路由器的配置 Router>enable Router#configure terminal En ...

  8. leetcode解题报告(1):Remove Duplicates from Sorted Array

    描述 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  9. 【概率论】3-3:累积分布函数(Cumulative Distribution Function)

    title: [概率论]3-3:累积分布函数(Cumulative Distribution Function) categories: Mathematic Probability keywords ...

  10. mysql 查询整个数据库所有表的行数

    >use information_schema; >select sum(table_rows) from tables where TABLE_SCHEMA = "test&q ...