Detailed description of the annotation-driven event listener in the Spring 4.2 framework
Detailed description of the annotation-driven event listener in the Spring 4.2 framework
Event interaction has become an indispensable part of many applications. The Spring framework provides a complete infrastructure to handle transient events. Next let's take a look at the annotation-driven event listener in the Spring 4.2 framework.
1. Early approaches
In the early stage, if the component needs to obtain notifications from the custom domain events of the Spring event, the component must implement the ApplicationListener interface and override the onApplicationEvent method.
@Componentclass OldWayBlogModifiedEventListener implements ApplicationListener
{ (...) @Override public void onApplicationEvent(OldWayBlogModifiedEvent event) { externalNotificationSender.oldWayBlogModified(event); }}
The code above works normally, but it creates a new class for each event, resulting in code bottlenecks.
In addition, our event class inherits the ApplicationEvent class-the event base class in the Spring application.
class OldWayBlogModifiedEvent extends ApplicationEvent { public OldWayBlogModifiedEvent(Blog blog) { super(blog); } public Blog getBlog() { return (Blog)getSource(); }}
Note that using Domain objects in events has obvious drawbacks and is unavailable in some scenarios. Here is just a sample code.
By the way, the ExternalNotificationSender object is responsible for sending external notifications to registered users (such as by email or SMS ).
2. annotation-driven event listener
Note that the Spring 4.2 framework uses annotation @ EventListener to annotate any Spring component.
@EventListenerpublic void blogModified(BlogModifiedEvent blogModifiedEvent) { externalNotificationSender.blogModified(blogModifiedEvent);}
Spring creates an ApplicationListener instance for the event and obtains the event type from the method parameters. There is no limit on the number of methods annotated by events in a class. All related event handles are grouped into one class.
3. Conditional event handling
To make the annotation @ EventListener more powerful, Spring 4.2 supports using SpEL expressions to express the event type. Assume the following is an event class:
public class BlogModifiedEvent { private final Blog blog; private final boolean importantChange; public BlogModifiedEvent(Blog blog) { this(blog, false); } public BlogModifiedEvent(Blog blog, boolean importantChange) { this.blog = blog; this.importantChange = importantChange; } public Blog getBlog() { return blog; } public boolean isImportantChange() { return importantChange; }}
Note that there may be no such hierarchical events in the actual application.
Note that writing in Groovy is easier.
Use Condition Parameters to describe events. Important changes are:
@EventListener(condition = #blogModifiedEvent.importantChange)public void blogModifiedSpEL(BlogModifiedEvent blogModifiedEvent) { externalNotificationSender.blogModifiedSpEL(blogModifiedEvent);}
4. Loose the layered structure of event types
In versions earlier than Spring 4.2, ApplicationEventPublisher is only capable of releasing its inherited objects after the ApplicationEvent event. In Spring 4.2, this interface has been extended to support any object type. In this case, the object is encapsulated into PayloadApplicationEvent and sent.
//base class with Blog field - no need to extend `ApplicationEvent`class BaseBlogEvent {}class BlogModifiedEvent extends BaseBlogEvent {}//somewhere in the codeApplicationEventPublisher publisher = (...); //injectedpublisher.publishEvent(new BlogModifiedEvent(blog)); //just plain instance of the event
This change makes publishing events easier. On the other hand, however, it can make event tracking more difficult, especially in large applications.
5. Respond to release events
Note @ EventListener another note: when the return type is not empty, Spring Automatically releases the returned event.
@EventListenerpublic BlogModifiedResponseEvent blogModifiedWithResponse(BlogModifiedEvent blogModifiedEvent) { externalNotificationSender.blogModifiedWithResponse(blogModifiedEvent); return new BlogModifiedResponseEvent( blogModifiedEvent.getBlog(), BlogModifiedResponseEvent.Status.OK);}
6. asynchronous event processing
Annotation @ EventListener can also be combined with annotation @ Async to provide an asynchronous event processing mechanism. In the following code, the specified event listener neither blocks major code execution nor is processed by other listeners.
@Async //Remember to enable asynchronous method execution //in your application with @EnableAsync@EventListenerpublic void blogAddedAsync(BlogAddedEvent blogAddedEvent) { externalNotificationSender.blogAdded(blogAddedEvent);}
For Asynchronous execution of work, you usually need to use annotation @ EnableAsync in the context of the Spring Project.
7. Summary
The annotation-driven event listener is a new feature introduced in Spring framework 4.2. It reduces the sample code of Spring projects and makes the code more flexible, this is especially evident in the demand for small numbers of events.