概念
参考链接: https://blog.csdn.net/qq_37758497/article/details/118863308
代码案例
测试主类
/**
* https://blog.csdn.net/qq_37758497/article/details/118863308
* 事件控制器,根据不同的类型选择不同的事件发布服务
*/
@RestController
@RequestMapping("/eventController")
@Slf4j
@RequiredArgsConstructor
public class EventController {
@Autowired
private PublishEventA publishEventA;
@Autowired
private PublishEventB publishEventB;
@Autowired
private PublishEventC publishEventC;
/**
* 发布
* @return
*/
@RequestMapping("pub")
public void pub(String publishType) {
if (StringUtils.endsWithIgnoreCase(publishType, "A")){
publishEventA.publish("你若为我繁华,你好呀:" + (publishType + 1));
}else if (StringUtils.endsWithIgnoreCase(publishType, "B")){
publishEventB.publish("你若为我繁华,你好呀:" + (publishType + 1));
}else if (StringUtils.endsWithIgnoreCase(publishType, "C")){
publishEventC.publish("你若为我繁华,你好呀:" + (publishType + 1));
}
}
}
三个事件发布服务
/**
* 发布方式A,发布TestEventA
* 直接注入ApplicationContext
*/
@Service
public class PublishEventA {
@Autowired
private ApplicationContext applicationContext;
public void publish(String message) {
applicationContext.publishEvent(new TestEventA(message));
}
}
/**
* 发布方式B,发布TestEventB
* 直接注入ApplicationEventPublisher:
*/
@Service
public class PublishEventB {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publish(String message) {
applicationEventPublisher.publishEvent(new TestEventB(message));
}
}
/**
* 实现ApplicationEventPublisherAware,通过ApplicationEventPublisher发布
*
* 发布方式C,发布TestEventC
*/
@Service
public class PublishEventC implements ApplicationEventPublisherAware {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publish(String message) {
applicationEventPublisher.publishEvent(new TestEventC(message));
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher=applicationEventPublisher;
}
}
三个事件测试类,为了用不通的监听去触发
/**
* ApplicationEvent:应用事件,职责为定义业务
*
* Spring 提供了一个继承于java.util.EventObject 类的ApplicationEvent的的抽象类,
* 并提供了应用上线文事件的抽象实现ApplicationContextEvent 下面的容器关闭、刷新、启动、停止等容器事件
* 以及RequestHandledEvent(http 请求处理完成事件),可自定义
* 事件(只需要实现ApplicationEvent 抽象类定义有参构造函数即可,source表示事件源,( 可按照自己的需求制定)
*
* ApplicationListener:事件监听器,职责为处理事件广播器发布的事件。
* Spring提供了继承于java.util.EventListener接口的应用监听器接口, ApplicationListener
* 并提供了两个实现:SmartApplicationListener和GenericApplicationListener接口。
*/
public class TestEventA extends ApplicationEvent {
private String message;
public TestEventA(Object source, String message) {
super(source);
this.message = message;
}
public TestEventA(String message) {
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
/**
* ApplicationEvent:应用事件,职责为定义业务
*
* Spring 提供了一个继承于java.util.EventObject 类的ApplicationEvent的的抽象类,
* 并提供了应用上线文事件的抽象实现ApplicationContextEvent 下面的容器关闭、刷新、启动、停止等容器事件
* 以及RequestHandledEvent(http 请求处理完成事件),可自定义
* 事件(只需要实现ApplicationEvent 抽象类定义有参构造函数即可,source表示事件源,( 可按照自己的需求制定)
*
* ApplicationListener:事件监听器,职责为处理事件广播器发布的事件。
* Spring提供了继承于java.util.EventListener接口的应用监听器接口, ApplicationListener
* 并提供了两个实现:SmartApplicationListener和GenericApplicationListener接口。
*/
public class TestEventB extends ApplicationEvent {
private String message;
public TestEventB(Object source, String message) {
super(source);
this.message = message;
}
public TestEventB(String message) {
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
/**
* ApplicationEvent:应用事件,职责为定义业务
*
* Spring 提供了一个继承于java.util.EventObject 类的ApplicationEvent的的抽象类,
* 并提供了应用上线文事件的抽象实现ApplicationContextEvent 下面的容器关闭、刷新、启动、停止等容器事件
* 以及RequestHandledEvent(http 请求处理完成事件),可自定义
* 事件(只需要实现ApplicationEvent 抽象类定义有参构造函数即可,source表示事件源,( 可按照自己的需求制定)
*
* ApplicationListener:事件监听器,职责为处理事件广播器发布的事件。
* Spring提供了继承于java.util.EventListener接口的应用监听器接口, ApplicationListener
* 并提供了两个实现:SmartApplicationListener和GenericApplicationListener接口。
*/
public class TestEventC extends ApplicationEvent {
private String message;
public TestEventC(Object source, String message) {
super(source);
this.message = message;
}
public TestEventC(String message) {
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
三个监听去处理业务
/**
* 事件机制监听的方式有两种:
* 监听方式1,监听事件TestEventA
* 实现ApplicationListener接口
*/
@Component
@Slf4j
public class EventListenerA implements ApplicationListener<TestEventA> {
@Override
public void onApplicationEvent(TestEventA event) {
//逻辑处理
log.info("监听到数据A:{}", event.getMessage());
}
}
/**
* 事件机制监听的方式有两种:
* 监听方式2,监听事件TestEventB
* EventListener注解形式
*/
@Component
@Slf4j
@EnableAsync
public class EventListenerB {
@Async
@EventListener
public void listener(TestEventB event) throws InterruptedException {
Thread.sleep(2000);
log.info("监听到数据B:{}", event.getMessage());
}
}
/**
* 事件机制监听的方式有两种:
* 监听方式2,监听事件TestEventC
* EventListener注解形式
*/
@Component
@Slf4j
@EnableAsync
public class EventListenerC {
@Async
@EventListener
public void listener(TestEventC event) throws InterruptedException {
Thread.sleep(2000);
log.info("监听到数据C:{}", event.getMessage());
}
}