springboot 自定义监听事件

事件监听模式是一种常用的设计模式,在springboot 中我们如何实现呢?

首先我们要理解事件监听中需要的几个角色

  • 事件发布者 (即事件源)
  • 事件监听者
  • 事件本

定义事件本身

事件本身需要继承ApplicationEvent

package com.test.listener;

import org.springframework.context.ApplicationEvent;

import java.util.List;
import java.util.Map;

public class ApplicationEventTest extends ApplicationEvent {

	private String type;
	private List<Map> msg;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<Map> getMsg() {
		return msg;
	}

	public void setMsg(List<Map> msg) {
		this.msg = msg;
	}

	public ApplicationEventTest(Object source, String type, List<Map> msg) {
		super(source);
		this.type = type;
		this.msg = msg;
	}
}

定义事件源

事件源需要注入 ApplicationContext

package com.test.listener;

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

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class DemoPublisher {

	@Resource
	ApplicationContext applicationContext;

	public void publish(String type , List<Map> msg) {
		applicationContext.publishEvent(new ApplicationEventTest(this,type, msg ));
	}

}

定义监听者

监听者需要实现 ApplicationListener

package com.test.listener;

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

import java.util.List;
import java.util.Map;

@Component
public class DemoListener1 implements ApplicationListener<ApplicationEventTest> {

	@Override
	public void onApplicationEvent(ApplicationEventTest event) {
		List<Map> msg = event.getMsg();
		String type = event.getType();
		System.out.println("listener1 接收到了 publisher 发送的消息类型 :" + type +", 消息内容: " + msg);
	}

}

测试

启动类

package com.test;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 项目启动类
 */
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Controller

package com.test.controller;

import com.test.listener.DemoPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/demo")
public class DemoController {

	@Resource
	private DemoPublisher demoPublisher;

	@GetMapping("testListener")
	public String testListener() {
		ArrayList<Map> list = new ArrayList<>();
		HashMap<String, String> m1 =  new HashMap<>();
		m1.put("1", "2");
		HashMap<String, String> m2 =  new HashMap<>();
		m2.put("3", "4");
		HashMap<String, String> m3 =  new HashMap<>();
		m3.put("5", "6");
		list.add(m1);
		list.add(m2);
		list.add(m3);
		demoPublisher.publish("测试消息",list);
		return "消息发布成功";
	}

}

在这里插入图片描述
在这里插入图片描述

目录结构

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了一种自定义事件的机制,可以在应用程序中定义自己的事件,并在需要的时候发布这些事件自定义事件可以用于在应用程序中实现解耦,使得不同的组件之间可以松散地耦合在一起。下面是一个简单的示例,演示如何在Spring Boot应用程序中定义和发布自定义事件: 1.定义一个自定义事件类,继承自ApplicationEvent类,例如MyEvent: ```java public class MyEvent extends ApplicationEvent { private String message; public MyEvent(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } } ``` 2.定义一个事件发布者类,例如MyEventPublisher: ```java @Component public class MyEventPublisher { @Autowired private ApplicationEventPublisher publisher; public void publishEvent(String message) { publisher.publishEvent(new MyEvent(this, message)); } } ``` 3.定义一个事件监听器类,例如MyEventListener: ```java @Component public class MyEventListener { @EventListener public void onApplicationEvent(MyEvent event) { System.out.println("Received spring custom event - " + event.getMessage()); } } ``` 4.在需要发布事件的地方,注入MyEventPublisher并调用publishEvent方法即可: ```java @Autowired private MyEventPublisher publisher; ... publisher.publishEvent("Hello, world!"); ``` 这样,当应用程序发布MyEvent事件时,MyEventListener中的onApplicationEvent方法将被调用,并输出事件的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值