郑州网站优化外包,成品短视频app源码的下载方法,空气净化器用什么网站做外贸,阿里云购买网站登录前言 ApplicationEvent 与 ApplicationListener 应用 实现 基于注解 事件过滤 异步事件监听 好处及应用场景 源码阅读 总结
1前言
ApplicationContext 中的事件处理是通过 ApplicationEvent 类和 ApplicationListener 接口提供的。如果将实现了 ApplicationListener … 前言 ApplicationEvent 与 ApplicationListener 应用 实现 基于注解 事件过滤 异步事件监听 好处及应用场景 源码阅读 总结
1前言
ApplicationContext 中的事件处理是通过 ApplicationEvent 类和 ApplicationListener 接口提供的。如果将实现了 ApplicationListener 接口的 bean 部署到容器中则每次将 ApplicationEvent 发布到ApplicationContext 时都会通知到该 bean这简直是典型的观察者模式。设计的初衷就是为了系统业务逻辑之间的解耦提高可扩展性以及可维护性。
Spring 中提供了以下的事件 2ApplicationEvent 与 ApplicationListener 应用
实现
1、自定义事件类基于 ApplicationEvent 实现扩展
public class DemoEvent extends ApplicationEvent {private static final long serialVersionUID -2753705718295396328L;private String msg;public DemoEvent(Object source, String msg) {super(source);this.msg msg;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg msg;}
}2、定义 Listener 类实现 ApplicationListener接口并且注入到 IOC 中。等发布者发布事件时都会通知到这个bean从而达到监听的效果。
Component
public class DemoListener implements ApplicationListenerDemoEvent {Overridepublic void onApplicationEvent(DemoEvent demoEvent) {String msg demoEvent.getMsg();System.out.println(bean-listener 收到了 publisher 发布的消息: msg);}
}3、要发布上述自定义的 event需要调用 ApplicationEventPublisher 的 publishEvent 方法我们可以定义一个实现 ApplicationEventPublisherAware 的类并注入 IOC来进行调用。
Component
public class DemoPublisher implements ApplicationEventPublisherAware {private ApplicationEventPublisher applicationEventPublisher;Overridepublic void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {this.applicationEventPublisher applicationEventPublisher;}public void sendMsg(String msg) {applicationEventPublisher.publishEvent(new DemoEvent(this, msg));}
}4、客户端调用 publisher
RestController
RequestMapping(/event)
public class DemoClient implements ApplicationContextAware {private ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext applicationContext;}GetMapping(/publish)public void publish(){DemoPublisher bean applicationContext.getBean(DemoPublisher.class);bean.sendMsg(发布者发送消息......);}
}输出结果 bean-listener 收到了 publisher 发布的消息: 发布者发送消息...... 基于注解
我们可以不用实现 AppplicationListener 接口 在方法上使用 EventListener 注册事件。如果你的方法应该侦听多个事件并不使用任何参数来定义可以在 EventListener 注解上指定多个事件。
重写 DemoListener 类如下
public class DemoListener {EventListener(value {DemoEvent.class, TestEvent.class})public void processApplicationEvent(DemoEvent event) {String msg event.getMsg();System.out.println(bean-listener 收到了 publisher 发布的消息: msg);}
}事件过滤
如果希望通过一定的条件对事件进行过滤可以使用 EventListener 的 condition 属性。以下实例中只有 event 的 msg 属性是 my-event 时才会进行调用。
EventListener(value {DemoEvent.class, TestEvent.class}, condition #event.msg my-event)
public void processApplicationEvent(DemoEvent event) {String msg event.getMsg();System.out.println(bean-listener 收到了 publisher 发布的消息: msg);}此时发送符合条件的消息listener 才会侦听到 publisher 发布的消息。 bean-listener 收到了 publisher 发布的消息: my-event 异步事件监听
前面提到的都是同步处理事件那如果我们希望某个特定的侦听器异步去处理事件如何做
使用 Async 注解可以实现类内方法的异步调用这样方法在执行的时候将会在独立的线程中被执行调用者无需等待它的完成即可继续其他的操作。
EventListener
Async
public void processApplicationEvent(DemoEvent event) {String msg event.getMsg();System.out.println(bean-listener 收到了 publisher 发布的消息: msg);
}使用异步监听时有两点需要注意 如果异步事件抛出异常则不会将其传播到调用方。 异步事件监听方法无法通过返回值来发布后续事件如果需要作为处理结果发布另一个事件请插入 ApplicationEventPublisher 以手动发布事件
3好处及应用场景
ApplicationContext 在运行期会自动检测到所有实现了 ApplicationListener 的 bean并将其作为事件接收对象。当我们与 spring 上下文交互触发 publishEvent 方法时每个实现了 ApplicationListener 的 bean 都会收到 ApplicationEvent 对象每个 ApplicationListener 可以根据需要只接收自己感兴趣的事件。
这样做有什么好处呢
在传统的项目中各个业务逻辑之间耦合性比较强controller 和 service 间都是关联关系然而使用 ApplicationEvent 监听 publisher 这种方式类间关系是什么样的我们不如画张图来看看。
DemoPublisher 和 DemoListener 两个类间并没有直接关联解除了传统业务逻辑两个类间的关联关系将耦合降到最小。这样在后期更新、维护时难度大大降低了。 ApplicationEvent 使用观察者模式实现那什么时候适合使用观察者模式呢观察者模式也叫 发布-订阅模式例如微博的订阅我们订阅了某些微博账号当这些账号发布消息时我们都会收到通知。
总结来说定义对象间的一种一对多的依赖关系当一个对象的状态发生改变时所有依赖于它的对象都得到通知并被自动更新从而实现广播的效果。
4源码阅读 Spring中的事件机制流程
1、ApplicationEventPublisher是Spring的事件发布接口事件源通过该接口的pulishEvent方法发布事件
2、ApplicationEventMulticaster就是Spring事件机制中的事件广播器它默认提供一个SimpleApplicationEventMulticaster实现如果用户没有自定义广播器则使用默认的。它通过父类AbstractApplicationEventMulticaster的getApplicationListeners方法从事件注册表事件-监听器关系保存中获取事件监听器并且通过invokeListener方法执行监听器的具体逻辑
3、ApplicationListener就是Spring的事件监听器接口所有的监听器都实现该接口本图中列出了典型的几个子类。其中RestartApplicationListnener在SpringBoot的启动框架中就有使用
4、在Spring中通常是ApplicationContext本身担任监听器注册表的角色在其子类AbstractApplicationContext中就聚合了事件广播器ApplicationEventMulticaster和事件监听器ApplicationListnener并且提供注册监听器的addApplicationListnener方法
ChatGPT中文网站https://ai.cxyquan.com/
通过上图就能较清晰的知道当一个事件源产生事件时它通过事件发布器ApplicationEventPublisher发布事件然后事件广播器ApplicationEventMulticaster会去事件注册表ApplicationContext中找到事件监听器ApplicationListnener并且逐个执行监听器的onApplicationEvent方法从而完成事件监听器的逻辑。
来到ApplicationEventPublisher 的 publishEvent 方法内部
protected void publishEvent(Object event, Nullable ResolvableType eventType) {if (this.earlyApplicationEvents ! null) {this.earlyApplicationEvents.add(applicationEvent);}else {// getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);}
}多播事件方法
Override
public void multicastEvent(final ApplicationEvent event, Nullable ResolvableType eventType) {ResolvableType type (eventType ! null ? eventType : resolveDefaultEventType(event));Executor executor getTaskExecutor();// 遍历所有的监听者for (ApplicationListener? listener : getApplicationListeners(event, type)) {if (executor ! null) {// 异步调用监听器executor.execute(() - invokeListener(listener, event));}else {// 同步调用监听器invokeListener(listener, event);}}
}invokeListener
protected void invokeListener(ApplicationListener? listener, ApplicationEvent event) {ErrorHandler errorHandler getErrorHandler();if (errorHandler ! null) {try {doInvokeListener(listener, event);}catch (Throwable err) {errorHandler.handleError(err);}}else {doInvokeListener(listener, event);}
}doInvokeListener
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {try {// 这里是事件发生的地方listener.onApplicationEvent(event);}catch (ClassCastException ex) {......}
}点击 ApplicationListener 接口 onApplicationEvent 方法的实现可以看到我们重写的方法。 5总结
Spring 使用反射机制获取了所有继承 ApplicationListener 接口的监听器在 Spring 初始化时会把监听器都自动注册到注册表中。
Spring 的事件发布非常简单我们来总结一下 定义一个继承 ApplicationEvent 的事件 定义一个实现 ApplicationListener 的监听器或者使用 EventListener 监听事件 定义一个发送者调用 ApplicationContext 直接发布或者使用 ApplicationEventPublisher 来发布自定义事件
最后发布-订阅模式可以很好的将业务逻辑进行解耦上图验证过大大提高了可维护性、可扩展性。