SpringBoot事件监听

代码演示:

package com.boot.event.eventdemo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class EventDemoApplication { public static void main(String[] args) {
SpringApplication app = new SpringApplication(EventDemoApplication.class);
//第一种方式 添加监听事件
// app.addListeners(new MyApplicationListener());
ConfigurableApplicationContext context = app.run(args);
// 发布事件
context.publishEvent(new MyApplicationEvent(new Object())); context.close();
}
}
package com.boot.event.eventdemo; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; @Component
public class HandlerEvent {
//第四种方式,最常用方式
@EventListener(MyApplicationEvent.class)
public void handlerEvent(MyApplicationEvent event) {
System.out.println("接受到了事件====:"+event.getClass());
System.out.println("接受到了事件====:"+event.getSource());
} @EventListener(ContextClosedEvent.class)
public void handlerEvent1(Object event) {
System.out.println("接受到了事件:"+event.getClass());
}
}
package com.boot.event.eventdemo;
import org.springframework.context.ApplicationEvent;
public class MyApplicationEvent extends ApplicationEvent {
public MyApplicationEvent(Object source) {
super(source);
}
}
package com.boot.event.eventdemo; import org.springframework.context.ApplicationListener; //第二种方式 @Component
public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> { @Override
public void onApplicationEvent(MyApplicationEvent event) {
System.out.println("接受到了事件:"+event.getClass());
System.out.println("接受到了事件:"+event.getSource());
}
}
application.properties
#第三种方式
context.listener.classes=com.boot.event.eventdemo.MyApplicationListener
使用第四种方式配置监听器的打印结果:

SpringBoot事件监听的更多相关文章
- SpringBoot事件监听机制及观察者模式/发布订阅模式
目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- springboot 中事件监听模型的一种实现
目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...
- SpringBoot入门之事件监听
spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种: ApplicationStartingEvent Applicatio ...
- SpringBoot框架(6)--事件监听
一.场景:类与类之间的消息通信,例如创建一个对象前后做拦截,日志等等相应的事件处理. 二.事件监听步骤 (1)自定义事件继承ApplicationEvent抽象类 (2)自定义事件监听器,一般实现Ap ...
- SpringBoot Application事件监听
SpringBoot Application共支持6种事件监听,按顺序分别是: ApplicationStartingEvent:在Spring最开始启动的时候触发 ApplicationEnviro ...
- springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署
知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...
- SpringBoot的事件监听
事件监听的流程分为三步:1.自定义事件,一般是继承ApplicationEvent抽象类.2.定义事件监听器,一般是实现ApplicationListener接口.3.a.启动的时候,需要将监听器加入 ...
- Spring Boot实践——事件监听
借鉴:https://blog.csdn.net/Harry_ZH_Wang/article/details/79691994 https://blog.csdn.net/ignorewho/arti ...
随机推荐
- AS 3.1 项目打包成jar或aar
1.首先明白一个道理. Android Studio编译的时候会自动将项目生成jar和aar的,我一开始以为jar需要自己单独生成,其实AS已经自动生成了,网上找的很多资料都是一个复制的过程而已. 只 ...
- 成都Uber优步司机奖励政策(3月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- PHP用url传递数组
数组传递这么写: echo "<a href=2.php?info=".base64_encode(serialize($information))." > ...
- 卸载Oracle 11g
卸载oracle只需要执行deinstall.ba即可 运行.bat文件 按照提示步骤进行卸载 .bat文件所在目录需要手动删除,需要在程序管理器里面关掉Oracle的进程,在删除当前文件夹
- 编译chromium时下载gn.exe时出错的解决方案
天朝人写个代码真难,想要编译一下chromium,但是获取代码时各种坑,不是网速慢,就是网络联不通,真难玩. 本文针对下载gn.exe等工具时失败的解决方案. 原因1:gclient没有走代理,针对使 ...
- 「暑期训练」「基础DP」免费馅饼(HDU-1176)
题意与分析 中文题就不讲题意了.我是真的菜,菜出声. 不妨思考一下,限制了我们决策的有哪些因素?一,所在的位置:二,所在的时间.还有吗?没有了,所以设dp[i][j]" role=" ...
- ElasticSearch搜索引擎在JavaWeb项目中的应用
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- PHP使用Redis消息队列
1.redis安装 参考:菜鸟教程http://www.runoob.com/redis/redis-install.html 2.安装php的redis扩展 1)phpinfo()查看php版本信息 ...
- Selenium 入门到精通系列:四
Selenium 入门到精通系列 PS:鼠标右键.鼠标悬停.键盘操作方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- Linux命令应用大词典-第35章 终端
35.1 tty:显示当前连接到当前标准输入的终端设备文件名 35.2 consoletype:显示连接到标准输入的控制台类型 35.3 fgconsole:显示活动的虚拟终端数量 35.4 ming ...