org.springframework.context.ApplicationContext发送消息

1、创建消息的实体类

package com.demo;

/**
 * 监听的实体类
 *
 */
public class EventMessage {
    private String name;

    public EventMessage(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2、创建消息接收

package com.demo;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MessageEventListener {

    @EventListener(EventMessage.class)
    public void onApplicationEvent(EventMessage eventMessage) {
        System.out.println("监听器接收的消息:"+ eventMessage.getName());
    }
}

3、消息发送的controller

package com.demo;


import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MessageController {

    @Resource
    private ApplicationContext applicationContext;

    @RequestMapping("/messageListener")
    public String messageListener(){
        applicationContext.publishEvent(new EventMessage("你好帅"));
        return "success";
    }
}

4、使用postman触发

5、结果

相关推荐
醉颜凉17 天前
页面间对象传递的几种方法
java·面试·cookie·request·session·application
huisheng_qaq20 天前
【netty系列-08】深入Netty组件底层原理和基本实现
java·netty·context·eventloop·channelhandler·netty原理及实现
York199620 天前
tensorrt python接口输出每一层的耗时
开发语言·python·tensorrt·context·myprofiler
Commas.KM1 个月前
【JS|第25期】探索HTTP POST请求:请求体的演变与应用
开发语言·javascript·http·json·application·form-data·application/xml
亚林瓜子1 个月前
SpringBoot启动初始化配置之ApplicationRunner
java·spring boot·后端·application
小小工匠1 个月前
Spring Boot - 通过ServletRequestHandledEvent事件实现接口请求的性能监控
spring boot·application
wn5312 个月前
【Go - context 速览,场景与用法】
开发语言·后端·golang·context
酒茶白开水2 个月前
React二组件进阶
react.js·生命周期·context·通讯·props·render-props·高阶组件
放羊的牧码3 个月前
MacOS - 启动台(LaunchPad)缺少应用软件图标
macos·app·图标·application·launchpad·启动台缺少应用软件图标·消失 不见