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 ...
随机推荐
- Multimodal Machine Learning:A Survey and Taxonomy 综述阅读笔记
该笔记基于:Multimodal Machine Learning:A Survey and Taxonomy 该论文是一篇对多模态机器学习领域的总结和分类,且发表于2017年,算是相当新的综述了.老 ...
- Linux管道及I/O重定向
I/O: 系统设定 默认输入设备:标准输入,STDIN,0 默认输出设备:标准输出,STDOUT,1 标准错误输出:STDERR,2 属于不同的数据流 标准输入:键盘 标准输出和错误输出:显示器 I/ ...
- HDU - 6440(费马小定理)
链接:HDU - 6440 题意:重新定义加法和乘法,使得 (m+n)^p = m^p + n^p 成立,p是素数.,且satisfied that there exists an integer q ...
- 254. Drop Eggs【LintCode java】
Description There is a building of n floors. If an egg drops from the k th floor or above, it will b ...
- HTMLTestRunner带饼图
# -*- coding: utf-8 -*- """ A TestRunner for use with the Python unit testing framewo ...
- 理解 JavaScript 原型 / 原型链
关于对象 以下代码中 p 的值是一个新对象,里面拥有 name 和 age 属性 function People(name, age){ this.name = name this.age = age ...
- JavaScript 常用控制流程代码范例
if-else 的用法 var a = 33 if (a == 1){ console.log ('a等于1') } else if (a==2) { console.log ('a等于2') } e ...
- java核心技术 笔记
一 . 总览 1. 类加载机制:jdk内嵌的class_loader有哪些,类加载过程.--后面需要补充 2. 垃圾收集基本原理,常见的垃圾收集器,各自适用的场景.--后面需要补充 3. 运行时动态编 ...
- js经典试题之运算符的优先级
js经典试题之运算符 1.假设val已经声明,可定义为任何值.则下面js代码有可能输出的结果为: console.log('Value is ' + (val != '0') ? 'define' : ...
- Scala可变对象
Java提供JavaBean作为数据对象的封装, 而对于Scala来说也提供了同样的支持. class Apple { var weight: Float = _ var color: String ...