代码演示:

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事件监听的更多相关文章

  1. SpringBoot事件监听机制及观察者模式/发布订阅模式

    目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...

  2. SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)

    SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...

  3. springboot 中事件监听模型的一种实现

    目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...

  4. SpringBoot入门之事件监听

    spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种: ApplicationStartingEvent Applicatio ...

  5. SpringBoot框架(6)--事件监听

    一.场景:类与类之间的消息通信,例如创建一个对象前后做拦截,日志等等相应的事件处理. 二.事件监听步骤 (1)自定义事件继承ApplicationEvent抽象类 (2)自定义事件监听器,一般实现Ap ...

  6. SpringBoot Application事件监听

    SpringBoot Application共支持6种事件监听,按顺序分别是: ApplicationStartingEvent:在Spring最开始启动的时候触发 ApplicationEnviro ...

  7. springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署

    知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...

  8. SpringBoot的事件监听

    事件监听的流程分为三步:1.自定义事件,一般是继承ApplicationEvent抽象类.2.定义事件监听器,一般是实现ApplicationListener接口.3.a.启动的时候,需要将监听器加入 ...

  9. Spring Boot实践——事件监听

    借鉴:https://blog.csdn.net/Harry_ZH_Wang/article/details/79691994 https://blog.csdn.net/ignorewho/arti ...

随机推荐

  1. ggplot2画简单的heatmap

    gg_heatmap gg_heatmap PeRl ggplot2 heatmap 偶然的机会,发现ggplot2画的heatmap也挺好看的,除了不能画出聚类树来(手动滑稽). 随意新建了两个矩阵 ...

  2. Java技术——Iterator和Enumeration的不同

    个函数接口.Iterator除了能读取集合的数据之外,也能对数据进行删除操作.尽管前者还没有被弃用但是已经被后者所代替了,Enumeration已经过时的,之所以没有被弃用是因为它仍被几种从以前版本遗 ...

  3. 北京Uber优步司机奖励政策(1月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...

  5. vue中开发webSocket

    先安装 sockjs-client 和 stompjs npm install sockjs-client npm install stompjs <template> <div&g ...

  6. 数据库路由中间件MyCat - 背景篇(2)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. MyCat的前世今生 如前文所说,Amoeba.Cobar.MyCat等属于同宗一脉.若Amoeba能继续下 ...

  7. android学习五 Intent

    1.Intent是组件间调用的桥梁. 2.Android系统定义了很多Intent    http://developer.android.com/guide/components/intents-c ...

  8. unity3d 角色头顶信息3D&2D遮挡解决方案(一)

    先上效果图,只凭文字描述,脑补应该有些困难- - 如图:有三个角色(我们暂且从左到右叫它们A.B.C),一个2D UI(中间动作选择的框框),一个cube(右边的方块) cube挡住了角色C的头顶信息 ...

  9. PS 去皱纹

    1.打开一个有皱纹的图片,选择修复画笔工具,按住Alt键吸取一块光滑的皮肤,然后再在有皱纹的位置上点击即可

  10. 「国庆训练」Kingdom of Obsession(HDU-5943)

    题意 给定\(s,n\),把\(s+1,s+2,...,s+n\)这\(n\)个数填到\(1,2,...,n\)里,要求\(x\)只能填到\(x\)的因子的位置(即题目中\(x\%y=0\)那么x才能 ...