I recently discovered Event Handling in Spring Framework and I'd like to understand the features and use cases of this particular instrument.
At a first glance they seem very similar to Aspects, but maybe I'm not seeing the big picture.
Are event an alternative to AOP or are they based on it? Are there use cases in which events are better than 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. 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.
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.
From a behavioral perspective, Aspects and Events are opposites. Aspects inject one piece of code when a point of logic is reached. 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. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With