分享如何在Spring/SpringBoot中实现观察者模式。循环编程再也不用面试了,Spring框架自带的事件监听机制,实现观察者模式,为你轻松实现解耦!Spring事件监听机制其实在Spring/SpringBoot框架中就有一套事件监听机制,可以实现观察者模式。Spring/SpringBoot框架中也有很多内置的事件,我们也可以自定义发布应用事件,下面会介绍。主要涉及的核心类和接口如下:ApplicationEventApplicationEvent(应用事件)是一个抽象类,相当于观察者模式下的观察目标。ApplicationEvent源码如下:publicabstractclassApplicationEventextendsEventObject{/**useserialVersionUIDfromSpring1.2forinteroperability.*/privatestaticfinallongserialVersionUID=7099057708183571937L;/**Systemtimewhentheeventhappened.*/privatefinallongtimestamp;/***Createanew{@codeApplicationEvent}.*@paramsourcetheobjectonwhichtheeventinitiallyoccurredorwith*whichtheeventisassociated(never{@codenull})*/publicApplicationEvent(Objectsource){super(source);this.timestamp=System.currentTimeMillis();}/***Returnthesystemtimeinmillisecondswhentheeventoccurred.*/publicfinallonggetTimestamp(){returnthis.timestamp;}}ApplicationEvent继承自Java中的EventObject事件对象类,Spring框架中的所有事件都继承自ApplicationEvent类,它是所有事件的父类。ApplicationEvent的主要核心是类构造器,它可以初始化一个源事件关联对象,在事件监听器中获取和通知更新。ApplicationListenerApplicationListener(应用程序事件监听器)是一个接口,相当于观察者模式下的观察者。ApplicationListener源码如下:publicinterfaceApplicationListener Suchaneventpublicationstepiseffectivelyahand-offtothe*multicasteranddoesnotimplysynchronous/asynchronousexecution*orevenimmediateexecutionatall.Eventlistenersareencouraged*tobeasefficientaspossible,individuallyusingasynchronous*executionforlonger-runningandpotentiallyblockingoperations.*@parameventtheeventtopublish*@see#publishEvent(Object)*@seeorg.springframework.context.event.ContextRefreshedEvent*@seeorg.springframework.context.event.ContextClosedEvent*/defaultvoidpublishEvent(ApplicationEventevent){publishEvent((Object)event);}/***通知所有匹配注册了这个*applicationofanevent的监听器。* 如果指定的{@codeevent}不是{@linkApplicationEvent},*itiswrappedina{@linkPayloadApplicationEvent}.* Suchaneventpublicationstepiseffectivelyahand-offtothe*multicasteranddoesnotimplysynchronous/asynchronousexecution*orevenimmediateexecutionatall.Eventlistenersareencouraged*tobeasefficientaspossible,individuallyusingasynchronous*executionforlonger-runningandpotentiallyblockingoperations.*@parameventtheeventtopublish*@since4.2*@see#publishEvent(ApplicationEvent)*@seePayloadApplicationEvent*/voidpublishEvent(Objectevent);}ApplicationEventPublisher有一个默认接口方法和一个接口方法。接口方法需要具体的子类容器来实现。如下图所示,ApplicationContext接口继承了ApplicationEventPublisher接口,所以可以使用常用的ApplicationContext来发布事件。上面介绍的Spring事件监听和发布角色结合起来发布ApplicationEvent事件,通过ApplicationEventPublisher或者ApplicationContext容器关联事件对象,然后ApplicationListener监听事件。当事件发布时,监听器将执行它并获得事件和关联的对象。SpringBootObserverMode了解了Spring框架中的事件和监听机制之后,我们就以上一篇文章中的ObserverMode为例进行改造。SpringBoot的基础知识和构建过程就不做介绍了。不熟悉的可以关注公众号Java技术栈,后台回复关键词“boot”阅读我之前写的系列教程。所有SpringBoot教程实战源码在以下仓库:https://github.com/javastacks/spring-boot-best-practice新建观察者目标类importlombok.Getter;importorg.springframework.context.ApplicationEvent;/***观察对象:栈长*来源微信公众号:Java技术栈{super(source);}}实现了Spring框架中的ApplicationEvent应用事件接口,相当于一个observertarget。添加观察者类importlombok.NonNull;importlombok.RequiredArgsConstructor;importorg.springframework.context.ApplicationListener;importorg.springframework.scheduling.annotation.Async;*/@RequiredArgsConstructorpublicclassReaderListenerimplementsApplicationListener