spring事件发送监听由3个部分组成
1.ApplicationEvent:表示事件本身,自定义事件需要继承该类
2.ApplicationEventPublisherAware:事件发送器,需要实现该接口
3.ApplicationListener<T>:事件监听器接口
注意!默认情况下是同步的,事件被publish后会等待Listener的处理
以下是实现代码
ApplicationEvent
public class MessageEvent extends ApplicationEvent {
public MessageEvent(Object source) {
super(source);
}
}
ApplicationEventPublisherAware
@Component
public class EventPublisher implements ApplicationEventPublisherAware {
private static ApplicationEventPublisher applicationEventPublisher;
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
EventPublisher.applicationEventPublisher = applicationEventPublisher;
}
public static void publishEvent(Appli