简体   繁体   English

春季活动与Aspects

[英]Spring events vs Aspects

I recently discovered Event Handling in Spring Framework and I'd like to understand the features and use cases of this particular instrument. 我最近在Spring Framework中发现了事件处理,我想了解这个特定仪器的功能和用例。

At a first glance they seem very similar to Aspects, but maybe I'm not seeing the big picture. 乍一看,它们看起来与Aspects非常相似,但也许我看不到大局。

Are event an alternative to AOP or are they based on it? 事件是AOP的替代品还是基于它? Are there use cases in which events are better than Aspects? 是否存在事件优于Aspects的用例?

Event Handling and Aspects are similar in purpose, but different in design. 事件处理和方面的目的相似,但设计不同。 One is not an alternative for the other. 一个不是另一个的替代品。 I'll summarize some differences here, but it might benefit you to explore the two topics further to see the difference. 我将在这里总结一些差异,但是进一步探索这两个主题可能会让您受益。

AOP offers us a way to inject logic into join-points in our code. AOP为我们提供了一种将逻辑注入代码中的连接点的方法。 This means that across these points the program the same logic is performed. 这意味着在这些点上程序执行相同的逻辑 Spring has powerful AOP tools which allow us to inject code into many areas and execution points of an application. Spring具有强大的AOP工具,允许我们将代码注入应用程序的许多区域和执行点。

Event Handling offers us a way to notify listeners when something affects application context or requests are handled. 事件处理为我们提供了一种在某些事情影响应用程序上下文或处理请求时通知侦听器的方法 Because listeners are executing the logic, we are able to perform different logic across various parts of our application. 因为侦听器正在执行逻辑,所以我们能够在应用程序的各个部分执行不同的逻辑 Unlike Aspects, however, the "insertion points" are more limited as they only involve HTTP requests or application context. 然而,与Aspects不同,“插入点”更受限制,因为它们仅涉及HTTP请求或应用程序上下文。

From a behavioral perspective, Aspects and Events are opposites . 从行为的角度来看,方面和事件是对立的 Aspects inject one piece of code when a point of logic is reached. 当达到逻辑点时,Aspects会注入一段代码。 In contrast, a piece of code executes in response to a point of logic being reached. 相反,一段代码响应于到达逻辑点而执行。 A single aspect processes many times throughout an application while events are processed by any amount of listeners executing code based on the event. 单个方面在整个应用程序中处理多次,而事件由任何数量的基于事件执行代码的侦听器处理。

The only real circumstance an aspect could be interchangeable with an event is performing code when an HTTPRequest is serviced. 一个方面可以与事件互换的唯一真实情况是在服务HTTPRequest时执行代码。 The aspect would be injected at a join-point related to the servicing method while the event would be handled by a listener who was notified when the request was serviced. 该方面将在与服务方法相关的连接点处注入,而事件将由在请求被服务时被通知的监听器处理。

In my opinion, AOP is much more powerful than a listener pattern, but also not as dynamic. 在我看来,AOP比听众模式更强大,但也不像动态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM