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

首先自定义一个监听器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#中'??'符的使用

    ??  用于判断当前对象是否为null. 语法: 对象 ?? "当前对象为null时赋的默认值". string nullString = null; string Kong = ...

  2. python的logging日志模块(二)

    晚上比较懒,直接搬砖了. 1.简单的将日志打印到屏幕   import logging logging.debug('This is debug message') logging.info('Thi ...

  3. P2168 [NOI2015]荷马史诗 k叉哈夫曼树

    思路:哈夫曼编码 提交:1次(参考题解) 题解:类似合并果子$QwQ$ 取出前$k$小(注意如果叶子结点不满的话要补全),合并起来再扔回堆里去. #include<cstdio> #inc ...

  4. 【题解】狼和羊-C++

    Description 米基家的后院养着一群羊,米基由于疲劳睡着了,这时一群饿狼钻进了后院开始攻击羊群,后院是由许多个方格构成的长方形区域,每个方格中用字符'?'表示空地,'#'表示栅栏,'o'表示羊 ...

  5. OpenCV2.4.5 加 Cuda5.0在vs2010下工

    想用opencv结合gpu加速处理,想重新编译opencv结合cuda模块无奈总出错 在国外网站上搜到一个cmakelists比较靠谱,项目可以生成,但还没有测试程序把list贴出来 ######## ...

  6. 表单 Flask-WTF - 校验器

    1 wtforms内置的校验器 Class wtforms.validators.DataRequired(message=None)此验证器将会检测field是否输入了数值,实际上是进行了if fi ...

  7. 微信小程序之--(与唯品会来场粉红色的邂逅 ???)

    Welcome to miaomiaoXiong's segmentfault 微信小程序之--(与唯品会来场粉红色的邂逅 ???) 买买买,虽然双十二刚过,可是唯品会的折扣却是依然火爆.一打开页面, ...

  8. Windows系统配置Redis密码

    1.首先停止Redis服务,直接关掉Redis或者打开任务管理器查看有没有redis-server进程,有的话选中并结束任务. 2.打开配置文件redis.windows.conf和redis.win ...

  9. sh: 1: Syntax error: Bad fd number

    Start on Ubuntu 6.10,Using dash default(theDebian Almquist Shell) instead bash(the GNUBourne-Again S ...

  10. 区间dp括号匹配

    POJ2955 匹配则加一,不需要初始化 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> ...