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、结果

相关推荐
stereohomology12 天前
使用人工智能的新方向:prompt -context- harness
prompt·context·harness
deephub12 天前
Prompt、Context、Harness:AI Agent 工程的三层架构解析
人工智能·prompt·大语言模型·context
ly甲烷20 天前
大模型应用:从Prompt 与 Context 到 Harness Engineering 的演进
prompt·agent·context·harness
oscar99925 天前
打开Claude Code的黑匣子:一次会话中,上下文窗口里到底发生了什么?
context·claude code
qqxhb1 个月前
15|Prompt 结构化:目标-上下文-约束-输出格式
prompt·ai编程·context·output·结构化·goal·constraints
Olafur_zbj1 个月前
【AI】LLM上下文拼接
java·开发语言·spring·llm·context
源代码•宸3 个月前
Golang面试题库(Context、Channel)
后端·面试·golang·context·channel·sudog·cancelctx
没有bug.的程序员3 个月前
Spring Boot 启动原理:从 @SpringBootApplication 到自动配置深度解析
java·spring boot·后端·python·springboot·application
源代码•宸3 个月前
Golang原理剖析(context、context面试与分析)
开发语言·后端·算法·面试·golang·context·emptyctx
heartbeat..3 个月前
Spring 全局上下文实现指南:单机→异步→分布式
java·分布式·spring·context