spring boot 事件

本文介绍了Spring Boot事件,其为Bean间消息通信提供支持,一个Bean处理任务后可让另一Bean监听其发送的事件。还阐述了事件流程,包括自定义事件、定义监听器、用容器发布事件等,并给出示例,最后展示了启动运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot 事件

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

事件流程

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

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

示例

  1. 自定义事件
package com.example.demo.event;

import org.springframework.context.ApplicationEvent;

/**
 * 自定义事件
 */
public class DemoEvent extends ApplicationEvent {

    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;
    }
}

  1. 事件监听器
package com.example.demo.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 定义事件监听
 */
@Component
public class DemoListener implements ApplicationListener<DemoEvent> { // 指定监听的事件类型--DemoEvent
    @Override
    public void onApplicationEvent(DemoEvent demoEvent) { // 接受消息并处理

        String msg = demoEvent.getMsg();

        System.out.println("我(bean-demoListener)接收到了bean-demoPublisher发布的消息: = " + msg);
    }
}
  1. 事件发布类
package com.example.demo.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class DemoPulisher  {
    @Autowired
    private ApplicationContext applicationContext; // 注入Application用来发布事件

    public void publish(String msg) { // 发布事件
        applicationContext.publishEvent(new DemoEvent(this, msg));
    }
}
  1. 配置类
package com.example.demo.event;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.example.demo.event")
public class EventConfig {
}
  1. 调用
package com.example.demo.controller;

import com.example.demo.config.SpringContextUtil;
import com.example.demo.event.DemoPulisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    private DemoPulisher demoPulisher;

    @GetMapping("/event")
    public String event(String msg) {
        demoPulisher.publish(msg);
        return msg;
    }

}

启动并运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值