SpringBoot监听器使用
实现方式有2种:实现ApplicationListener 接口;添加@EventListener 注解。
必须要的角色:
1、事件(event)可以封装和传递监听器中要处理的参数,如对象或字符串,并作为监听器中监听的目标。
2、监听器(listener)具体根据事件发生的业务处理模块,这里可以接收处理事件中封装的对象或字符串。
3、事件发布者(publisher)事件发生的触发者。
接口实现:
自定义事件需要继承Spring的ApplicationEvent
自定义监听器需要实现ApplicationListener
自定义事件发布需要自动注入了ApplicationEventPublisher
注解实现:
自定义事件需要继承Spring的ApplicationEvent
自定义监听器,在自定义的方法上添加@EventListener
自定义事件发布需要自动注入了ApplicationEventPublisher
参考:
https://segmentfault.com/a/1190000039097608
MyEvent
import org.springframework.context.ApplicationEvent;
public class MyEvent extends ApplicationEvent {
private String msg;
public MyEvent(Object source) {
super(source);
}
public MyEvent(Object source, String msg) {
super(source);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
MyEventTwo
import org.springframework.context.ApplicationEvent;
public class MyEventTwo extends ApplicationEvent {
private String msg;
public MyEventTwo(Object source) {
super(source);
}
public MyEventTwo(Object source, String msg) {
super(source);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
MyAnnotationListener
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class MyAnnotationListener {
@EventListener(classes={MyEvent.class,MyEventTwo.class})
public void listener(ApplicationEvent applicationEvent){
if(applicationEvent instanceof MyEvent){
System.out.println("MyAnnotationListener - MyEvent:"+((MyEvent) applicationEvent).getMsg());
}
if(applicationEvent instanceof MyEventTwo){
System.out.println("MyAnnotationListener - MyEventTwo:"+((MyEventTwo) applicationEvent).getMsg());
}
}
@EventListener
public void listenerTestOne(MyEvent myEvent){
System.out.println("listenerTestOne"+myEvent.getMsg());
}
@EventListener
public void listenerTestTwo(MyEventTwo myEventTwo){
System.out.println("listenerTestTwo"+myEventTwo.getMsg());
}
}
MyEventPubLisher
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
public class MyEventPubLisher {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void pushListener(String msg) {
applicationEventPublisher.publishEvent(new MyEvent(this, msg));
}
public void pushListenerTwo(String msg) {
applicationEventPublisher.publishEvent(new MyEventTwo(this, msg));
}
}
ObserverController 测试接口
import com.tech.elasticsearch.observer.MyEventPubLisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/observer")
public class ObserverController {
@Autowired
private MyEventPubLisher myEventPubLisher;
@GetMapping("/publish1")
public void publisher(String msg){
myEventPubLisher.pushListener(msg);
}
@GetMapping("/publish2")
public void pushListenerTwo(String msg){
myEventPubLisher.pushListenerTwo(msg);
}
}
说明:在MyAnnotationListener类中
如果参数为ApplicationEvent applicationEvent
@EventListener(classes={MyEvent.class,MyEventTwo.class}),则只监听这2个类
@EventListener,则监听所有继承了ApplicationEvent的类
如果参数为MyEvent myEvent或者MyEventTwo myEventTwo
@EventListener,只监听参数类型的事件