SpringBoot -- 事件(Application Event)

  Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件。

  Spring的事件需要遵循如下流程:

  1. 自定义事件,集成ApplicationEvent。
  2. 定义事件监听器,实现ApplicationListener。
  3. 使用容器发布事件。

一、自定义事件

package com.cenobitor.appevent.event;

import org.springframework.context.ApplicationEvent;

public class DemoEvent extends ApplicationEvent {

    private static final long serialVersionUID = 1L;
    private String msg;

    public DemoEvent(Object source,String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

二、事件监听器

package com.cenobitor.appevent.listener;

import com.cenobitor.appevent.event.DemoEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
//实现ApplicationListener接口,并指定监听的事件类型
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {

    @Override  //使用onApplicationEvent方法对消息进行接收处理
    public void onApplicationEvent(DemoEvent demoEvent) {
        String msg = demoEvent.getMsg();
        System.out.println("(bean-demoListener)接收到了bean-demoPublisher发布的消息:"+msg);
    }
}

三、事件发布类

package com.cenobitor.appevent.publisher;

import com.cenobitor.appevent.event.DemoEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class DemoPublisher {
    @Autowired  //注入ApplicationContext用来发布事件
    ApplicationContext applicationContext;
  //使用ApplicationContext的publishEvent方法来发布
    public void publish(String msg){
        applicationContext.publishEvent(new DemoEvent(this,msg));
    }
}

四、运行

package com.cenobitor.appevent;

import com.cenobitor.appevent.publisher.DemoPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppeventApplication.class);
        DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
        demoPublisher.publish("hello application event");
        context.close();
    }

}

结果:(bean-demoListener)接收到了bean-demoPublisher发布的消息:hello application event

注:摘抄自《JavaEE开发的颠覆者SpringBoot 实战》。

原文地址:https://www.cnblogs.com/gdwkong/p/9309459.html

时间: 2024-08-30 10:45:56

SpringBoot -- 事件(Application Event)的相关文章

Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)

一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要让另一个Bean监听当前Bean所发送的事情. Spring的事件需要遵循如下流程: (1)自定义事件,集成ApplicationEvent. (2)定义事件监听器,实现ApplicationListener. (3)使用容器发布事件. 示例: 1.自定义事件. package com.ecwork

spring 事件(Application Event)

spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先自定义事件:你的事件需要继承 ApplicationEvent 2.定义事件监听器: 需要实现 ApplicationListener 3.使用容器对事件进行发布 以下例子是场景是注册的时候发送邮件的一个场景: 先定义事件: package com.foreveross.service.weixin

CL_GUI_ALV_GRID 触发PAI事件(Application event)

*&---------------------------------------------------------------------* *& Report Z_BARRY_ALV_GRID_EVENT *& *&---------------------------------------------------------------------* *& *& *&-------------------------------------

TI-RTOS 之 事件同步(Event, 类似semaphore)

TI-RTOS 之 事件同步(Event, 类似semaphore) Event 是类似Semaphore的存在,官方如下描述: SYS/BIOS events are a means of communication between Tasks and other threads such as Hwis, Swis, and other Tasks, or between Tasks and other SYS/BIOS objects. Other SYS/BIOS objects inc

SpringBoot事件监听

SpringBoot事件监听 springBoot事件监听流程 1 自定义事件,一般是继承ApplicationEvent抽象类 /** * 定义事件 **/ public class MyApplicationEvent extends ApplicationEvent { private static final long serialVersionUID = 787751246882182041L; public MyApplicationEvent(Object source) { su

jacascript 事件对象event

前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 在触发DOM上的某个事件时,会产生一个事件对象 event,这个对象中包含着所有与事件有关的信息.所有浏览器都支持 event 对象,但有兼容性问题. 获取事件对象 一般地,event 对象是事件程序的第一个参数.IE8及以下浏览器不支持: 另一种方法是直接使用 event 变量,firefox 浏览器不支持: 获取事件对象的常见兼容写法: <div id="box" style=&qu

C#事件(Event)学习日记

event 关键字的来由,为了简化自定义方法的构建来为委托调用列表增加和删除方法. 在编译器处理 event 关键字的时候,它会自动提供注册和注销方法以及任何必要的委托类型成员变量. 这些委托成员变量总是声明为私有的,因此不能直接从触发事件对象访问它们. 温馨提示:如果您对于委托不是很了解,您可以先看 C#委托(Delegate) ,这对您理解本章会有所帮助. 定义一个事件的步骤: 需要定义一个委托,它包含事件触发时将要调用方法 通过 event 关键字用相关委托声明这个事件 话不多说,我们来看

Cocos2d-X3.0 刨根问底(七)----- 事件机制Event源码分析

这一章,我们来分析Cocos2d-x 事件机制相关的源码, 根据Cocos2d-x的工程目录,我们可以找到所有关于事件的源码都存在放在下图所示的目录中. 从这个event_dispatcher目录中的文件命名上分析 cocos2d-x与事件相关的类一共有四种, Event, EventListener,EventDispatcher, Touch分别为 事件,事件侦听器,事件分发器,触摸 我们先从Event类开始. 打开CCEvent.h文件 /** * Base class of all ki

javaScript中的事件对象event

事件对象event,每当一个事件被触发的时候,就会随之产恒一个事件对象event,该对象中主要包括了关于该事件的基本属性,事件类型type(click.dbclick等值).目标元素target(我的理解是事件源对象,即触发该事件的dom元素)等,以及一些与该事件相关的方法.取消事件默认行为preventDefault().组织事件继续冒泡或捕获stopPropagation()等等,这里我仅仅列举了,项目中我用到的属性和方法. 既然事件被触发.就随之产生了一个event对象.笔者在IE中測试了