代码演示:

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. HDL代码风格建议(1)使用示例和IP

    Recommended HDL Coding Styles HDL coding styles can have a significant effect on the quality of resu ...

  2. 【机器学习笔记】循环神经网络RNN

    1. 从一个栗子开始 - Slot Filling 比如在一个订票系统上,我们的输入 "Arrive Taipei on November 2nd" 这样一个序列,我们设置几个槽位 ...

  3. crash:EXC_ARM_DA_ALIGN(关于内存对齐,memcpy)

    crash:EXC_ARM_DA_ALIGN(关于内存对齐,memcpy) 问题描述 在iOS game开发时做内存拷贝时出现了 crash:EXC_ARM_DA_ALIGN,debug版本不会出现, ...

  4. 爬虫——URL模块爬取糗事百科段子

    最简单的爬取网页找有用信息,难点应该是正则锁定有用信息部分,看了一些其他大神的正则,最后还是决定按照自己理解写一个,果然我头脑相对简单,写出来的粗糙而易理解,也完成了自己想要的需求,就这样了~ # - ...

  5. 使用python+selenium控制手工已打开的浏览器

    我们可以利用Chrome DevTools协议.它允许客户检查和调试Chrome浏览器. 打开cmd,在命令行中输入命令: chrome.exe --remote-debugging-port=922 ...

  6. Linux命令应用大词典-第33章 X Window

    33.1 xhost:X服务器的访问控制程序 33.2 xinit:X Window系统初始化 33.3 Xlsclients:在显示器中列出正在运行的客户端应用程序 33.4 xlsfonts:显示 ...

  7. Android intel X86 图像渲染

    最近几天有个项目需要在intel 芯片的系统上集成我们的视频通话软件.之前只是在ARM平台上使用,对于intel 没测试过,直接运行apk后,本端渲染的图像出错,渲染出的图像很像I420被作为RGB5 ...

  8. Django - day01 快速回忆ORM操作

    Django - day01 Model的增删改查找 得益于Django的ORM模型,用面向对象的思想来操作数据库使得数据库的操作一切变得简洁了很多. 0. 建表 在应用下的models.py中建立一 ...

  9. EditorGUI控件输入监听

    EditorGUI控件输入监听 在做编辑器开放的过程中,有时候要对用户输入进行判断和限制,但EditorGUI控件却没有触发回调,而是提供了一种麻烦的办法--使用EditorGUI.BeginChan ...

  10. Java开发工程师(Web方向) - 02.Servlet技术 - 第1章.Servlet

    第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...