Spring Event Publishing
Author:xiaolu
Overview:
Spring 's applicationcontext , based on Beanfactory, supports the release of events (including synchronization - default, and asynchronous events).
Event Publishing and Event listener processing are essentially a way to use the observer pattern. For The release of Spring events, there are three parts: event entities, event listeners, event publishers.
Event Entity:
The event entity defines what event and contains the parameter information to be passed. in Spring event Publishing, custom event entities need to inherit the applicationevent class. That is, all events are subclasses of the applicationevent class.
Event Listener:
An event listener, the part that is interested in an event, needs to do some logical processing after a particular event has occurred. event listeners in Spring need to implement the applicationlistener interface. the event is handled in its onapplicationevent function.
Event Publisher:
The event Publisher is the part that publishes the event, which pushes the start of the event process. in Spring, the publication of an event can be published using the publishevent method of the ApplicationContext class .
code Example:
1) Java configuration section (alternative to XML file configuration)
2) Event definition
here defines a Startevent Event, inherited from applicationevent
3) Event listener
Specifying the type of event to listen on by using generic parameters
4) Event release
through ApplicationContext publishevent to publish the event.
There it is.Eventutilsclass is actually possible through the OneLine ofContextto publish the event, but theWebprogram, we are unable to directly obtainApplicationContextof the object. (Here is the demo, so the directNewGets theApplicationContext). To publish an event, you need to haveApplicationContextobject. So how to getApplicationContextWhat about the object? In theSpring, if you implement theXawareinterface, thenSpringin the constructionBean, the correspondingXinjected into thisBean, so you only need to implement theApplicationcontextawarecan be.
here, Eventutils applicationcontextaware interface, so spring container in construction eventutils this bean applicationcontextaware interface because the applicationcontext instance injected in, we can use it to publish the event.
There is also a way to achieve Applicationeventpublisheraware interface,Spring will inject applicationeventpublisher Type object through which to publish the implementation. (In fact, the injected object is the applicationcontext object, because the applicationcontext interface inherits from Applicationeventpublisher interface.
online often give implementation is to achieve Applicationcontextaware/applicationeventpublisheraware interface to publish events because: to hold The ApplicationContext object.
synchronization and Asynchrony of events:
The above event release is the use of synchronization, if you want to use the asynchronous way, the so-called asynchronous is the event after the publication without waiting for the event listener to complete the return. The implementation is that the event listener uses the new thread instead of the publishing thread to execute the event-handling logic.
@Async and @EnableAsync
Set the @Async label on the handling method of the event listener class , and add @EnableAsync on the configuration file (Configuration Class) .
code example:
Event Listener :
Configuration class (configuration file)
The asyncconfigurer interface is implemented in Spring and the Executoris provided, otherwise an exception is thrown (in This interface is not implemented in Springboot).
There is also a way to implement Asynchrony:
If you feel that the listener implements the Applicationlistener<event> interface is intrusive and can be annotated without the need to implement an interface:
The effect is the same:
If you remove @Async and @EnableAsync, The result is:
you can see that both the main main thread used to execute (that is, the event handling logic of the publisher and listener who performed the event in the same thread, which is naturally synchronous).
Spring Event Publishing