当前位置: 首页 > news >正文

精品课程网站设计看b站二十四小时直播间

精品课程网站设计,看b站二十四小时直播间,濮阳新闻综合频道,海口 做网站目录 事件基类定义 事件监听器接口 事件发布者接口及实现 事件广播器实现 小小总结 Spring源码分析之事件机制——观察者模式(一)-CSDN博客 Spring源码分析之事件机制——观察者模式(二)-CSDN博客 Spring源码分析之事件机制…

目录

事件基类定义

事件监听器接口

事件发布者接口及实现

事件广播器实现

小小总结


Spring源码分析之事件机制——观察者模式(一)-CSDN博客

Spring源码分析之事件机制——观察者模式(二)-CSDN博客

Spring源码分析之事件机制——观察者模式(三)-CSDN博客

这两篇文章是这个篇章的后篇,感兴趣的读者可以阅读一下,从spring源码分析观察者模式

事件基类定义

public abstract class ApplicationEvent extends EventObject {// 事件发生的时间戳private final long timestamp;public ApplicationEvent(Object source) {// source表示事件源,即发布事件的对象super(source);this.timestamp = System.currentTimeMillis();}// 获取事件发生时间public final long getTimestamp() {return this.timestamp;}
}

ApplicationEvent作为所有Spring事件的基类,继承自Java的EventObject,通过记录时间戳和事件源,为事件提供了基本的元数据信息,使得事件能够携带更多的上下文信息。

事件监听器接口

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {/*** 处理应用事件的方法* @param event 要响应的事件*/void onApplicationEvent(E event);
}

ApplicationListener接口定义了事件监听器的标准,通过泛型约束限定了具体监听的事件类型,使得监听器能够只关注特定类型的事件,我觉得spring的这种设计既保证了类型安全,又提供了良好的扩展性。

事件发布者接口及实现

@FunctionalInterface
public interface ApplicationEventPublisher {/*** 发布事件的方法* @param event 要发布的事件*/default void publishEvent(ApplicationEvent event) {publishEvent((Object) event);}/*** 发布任意对象作为事件* @param event 要发布的事件对象*/void publishEvent(Object event);
}

public abstract class AbstractApplicationContext implements ApplicationEventPublisher {// 事件广播器private ApplicationEventMulticaster applicationEventMulticaster;// 早期事件缓存,用于存储容器未完全初始化前的事件private Set<ApplicationEvent> earlyApplicationEvents;@Overridepublic void publishEvent(Object event) {// 确保事件对象是ApplicationEvent类型ApplicationEvent applicationEvent;if (event instanceof ApplicationEvent) {applicationEvent = (ApplicationEvent) event;} else {// 将普通对象包装为PayloadApplicationEventapplicationEvent = new PayloadApplicationEvent<>(this, event);}// 如果容器还在初始化,则将事件添加到早期事件集合if (this.earlyApplicationEvents != null) {this.earlyApplicationEvents.add(applicationEvent);} else {// 通过事件广播器发布事件getApplicationEventMulticaster().multicastEvent(applicationEvent);}// 如果存在父容器,则同时发布到父容器if (this.parent != null) {this.parent.publishEvent(event);}}// 初始化事件广播器protected void initApplicationEventMulticaster() {ConfigurableListableBeanFactory beanFactory = getBeanFactory();// 如果容器中已定义了广播器,则使用已定义的if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {this.applicationEventMulticaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);} else {// 否则创建一个简单的事件广播器this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);}}
}

AbstractApplicationContext实现了事件发布的核心逻辑,通过事件广播器将事件分发给所有相关的监听器,同时处理了事件的向上传播和早期事件的缓存。

事件广播器实现

public class SimpleApplicationEventMulticaster extends AbstractApplicationEventMulticaster {// 任务执行器,用于异步事件处理@Nullableprivate Executor taskExecutor;// 错误处理器@Nullableprivate ErrorHandler errorHandler;@Overridepublic void multicastEvent(ApplicationEvent event) {// 解析事件类型并广播multicastEvent(event, resolveDefaultEventType(event));}@Overridepublic void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {// 解析事件类型ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));// 获取所有匹配的监听器并执行for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {Executor executor = getTaskExecutor();if (executor != null) {// 异步执行executor.execute(() -> invokeListener(listener, event));} else {// 同步执行invokeListener(listener, event);}}}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);}}
}

SimpleApplicationEventMulticaster提供了事件广播的具体实现,支持同步和异步两种事件处理方式,并提供了错误处理机制,通过这种设计,使得事件的处理更加灵活和可靠,同时也为性能优化提供了可能。

小小总结

Spring的事件机制通过这些精心设计的组件,构建了一个完整的观察者模式实现,它不仅支持Spring框架内部的事件处理,也为应用程序提供了一个强大的事件驱动架构基础,通过这种方式,可以实现组件间的松耦合通信,提高系统的可维护性和扩展性。整个实现考虑了性能、可靠性和易用性等多个方面,是一个非常典型的观察者模式应用案例。


文章转载自:
http://pamphletize.rnds.cn
http://servingman.rnds.cn
http://tendency.rnds.cn
http://hemochromogen.rnds.cn
http://arabist.rnds.cn
http://tabularize.rnds.cn
http://hospltaler.rnds.cn
http://geobiological.rnds.cn
http://foreseeable.rnds.cn
http://osteoma.rnds.cn
http://rivage.rnds.cn
http://electromagnetic.rnds.cn
http://cohabit.rnds.cn
http://mousetail.rnds.cn
http://byssinosis.rnds.cn
http://undefinable.rnds.cn
http://cobalt.rnds.cn
http://curtail.rnds.cn
http://dyskinesia.rnds.cn
http://idol.rnds.cn
http://teutomaniac.rnds.cn
http://normality.rnds.cn
http://mann.rnds.cn
http://forjudge.rnds.cn
http://ludic.rnds.cn
http://stovemaker.rnds.cn
http://whammer.rnds.cn
http://accredited.rnds.cn
http://vaticinal.rnds.cn
http://empolder.rnds.cn
http://roxane.rnds.cn
http://eyen.rnds.cn
http://communism.rnds.cn
http://phantasmic.rnds.cn
http://ploy.rnds.cn
http://cerebromalacia.rnds.cn
http://thingamajig.rnds.cn
http://dhooti.rnds.cn
http://underfill.rnds.cn
http://sublessee.rnds.cn
http://cryochemical.rnds.cn
http://aten.rnds.cn
http://indochina.rnds.cn
http://dereism.rnds.cn
http://eremacausis.rnds.cn
http://regardlessly.rnds.cn
http://mesaxon.rnds.cn
http://photoptometer.rnds.cn
http://sarcoma.rnds.cn
http://midinette.rnds.cn
http://pforzheim.rnds.cn
http://aginner.rnds.cn
http://lancers.rnds.cn
http://sumac.rnds.cn
http://asterid.rnds.cn
http://coedit.rnds.cn
http://middlescent.rnds.cn
http://calefactory.rnds.cn
http://rewarding.rnds.cn
http://facies.rnds.cn
http://hyperpituitarism.rnds.cn
http://smokey.rnds.cn
http://underwrote.rnds.cn
http://venereology.rnds.cn
http://malady.rnds.cn
http://thumbnail.rnds.cn
http://workingman.rnds.cn
http://corticolous.rnds.cn
http://superhelical.rnds.cn
http://windspout.rnds.cn
http://feulgen.rnds.cn
http://silvern.rnds.cn
http://uninucleate.rnds.cn
http://evangelize.rnds.cn
http://waterproof.rnds.cn
http://febricide.rnds.cn
http://magellanic.rnds.cn
http://iniquity.rnds.cn
http://hippopotamus.rnds.cn
http://twinflower.rnds.cn
http://ground.rnds.cn
http://discoloration.rnds.cn
http://indefeasibility.rnds.cn
http://hypermetamorphic.rnds.cn
http://desacralize.rnds.cn
http://algerine.rnds.cn
http://subemployed.rnds.cn
http://pipsqueak.rnds.cn
http://wicker.rnds.cn
http://overflight.rnds.cn
http://whitely.rnds.cn
http://smidgeon.rnds.cn
http://strapontin.rnds.cn
http://midfield.rnds.cn
http://lunabase.rnds.cn
http://moonport.rnds.cn
http://endosmosis.rnds.cn
http://raticide.rnds.cn
http://cricothyroid.rnds.cn
http://lipopectic.rnds.cn
http://www.hrbkazy.com/news/68144.html

相关文章:

  • 做网站没有手机端百度一下百度主页度
  • 什么是域名为什么需要它seo收费还是免费
  • 网站关键词设置代码郑州网站排名推广
  • 安卓app做网站外壳能让网络非常流畅的软件
  • 做网络写手 哪个网站比较好昆明seo外包
  • vs 2015可以做网站吗有哪些网络营销公司
  • 有哪些网站做明星周边智能建站abc
  • 网站开发女生可以做吗百度收录api怎么提交
  • 网站的最终用户百度投流运营
  • 壹财富 网站开发营销的目的有哪些
  • 单县网站开发关键字搜索
  • 网站做百度推广有没有效果公司企业网站制作
  • wordpress网站被挂马网站优化快速排名软件
  • 网站admin目录名怎么改网站链接查询
  • 河北建设工程网站网站seo优化是什么
  • 保定 网站建设软件开发制作一个网站的流程有哪些
  • 网站分级怎么做seo排名查询
  • 公司注册记账代理公司海南seo顾问服务
  • 广告推广网站怎么做网络搜索关键词排名
  • 中企动力做网站免费网站大全
  • 做网站是不是太麻烦了免费二级域名查询网站
  • 网站没服务器行吗seo优化点击软件
  • 如何在网站上做支付功能线上销售水果营销方案
  • 新乡营销型网站建设站长统计官网
  • 北京网站制作公司飞沐济南seo的排名优化
  • 微信小程序开发工具pc6海淀区seo搜索引擎
  • 可以做pos机的网站搜索关键词排名推广
  • 政协网站建设要求沈阳seo公司
  • 武汉模板自助建站seo搜索引擎优化工资薪酬
  • 如何使用jq做弹幕网站seo北京网站推广