事件
自定义事件
public class LogEvent extends ApplicationEvent {
....
}
发布事件
- 获取ApplicationEventPublisher的实现类(如ApplicationContext).publishEvent
监听事件
@EventListener
@Async // 异步监听(需要开启异步配置)
public void accept(LogEvent event){}
// 多事件监听
@EventListener(classes = {MyEvent.class, ContextRefreshedEvent.class, ContextClosedEvent.class})
public void onMyEventPublished(ApplicationEvent event) {...}
public class LogListener implements ApplicationListener<LogEvent> {
@Async
@Override
public void onApplicationEvent(LogEvent logEvent) {
...
}
}