记录一下后端开发过程中学到的知识
# 事件发送与监听
- 首先需要自定义事件类,如下所示:
、、、
public class CustomEvent extends ApplicationEvent {
private String a;
public CustomEvent (Object source, String a){
super (source);
this.a = a;
}
public String getA (){
return a;
}
}
、、、
之后编写用于推送事件的 Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class EventPublisherService implements ApplicationEventPublisherAware{
private ApplicationEventPublisher eventPublisher;
@Override
public void setApplicationEventPublisher (ApplicationEventpublisher applicaitonEventPublisher){
this.eventPublisher = applicationEventPublisher;
}
public void publishPlayEvent (String a){
CustomEvent customEvent = new CustomEvent (this, a);
eventPublisher.publishEvent (customEvent);
}
}最后编写监听器来实现操作:
1
2
3
4
5
6
@EventListener
public void handleChannelEvent (CustomEvent customEvent){
String a= customEvent.a;
log.info (a);
}