Key members of the event mechanism:
- Event
- Event listeners (listen for event triggers, handle things)
- Event Source (Publish event)
Javase provides the standard for a range of custom events.
Evenobject, the base class for event types provided for Javase, any custom event must inherit from it.
EventListener, the base class for event listeners provided for Javase, any custom event listeners are implemented.
Javase does not provide an event publisher, which is the role of each application to implement the event publisher itself.
Spring provides the Applicationeventpublisher interface as the event Publisher, and the ApplicationContext interface inherits the interface and acts as the publisher of the event. However, the specific implementation of the ApplicationContext interface is different.
Spring provides the Applicationeventmulticaster interface for managing Applicationlistener and publishing Applicationevent. The ApplicationContext interface delegates the related work of the event to the Applicationeventmulticaster implementation class,
Spring Event mechanism Flow:
- ApplicationContext Publishing Events
Publish event context.publishevent;
The behavior of publishing events is actually performed by ApplicationContext implementation Abstractapplicationcontext
- Find event broadcaster, broadcast event
Getapplicationeventmulticaster (). Multicastevent (Applicationevent, EventType);
- Executes the Multicastevent method in Simpleapplicationeventmulticaster, invoking the Onapplicationevent () method of the event listener
@Overridepublic void Multicastevent (Final applicationevent event, Resolvabletype eventtype) {Resolvabletype type = ( EventType! = null? Eventtype:resolvedefaulteventtype (event)); for (Final applicationlistener<?> listener: Getapplicationlisteners (event, type)) {Executor Executor = Gettaskexecutor (); if (Executor! = null) {Executor.execute ( New Runnable () {@Overridepublic void run () {Invokelistener (Listener, event);}}); else {Invokelistener (listener, event);}}}
protected void Invokelistener (applicationlistener<?> Listener, applicationevent event) {ErrorHandler ErrorHandler = Geterrorhandler (); if (ErrorHandler! = null) {try {Doinvokelistener (listener, event);} catch (Throwable err) {errorhandler.handleerror (err);}} else {Doinvokelistener (listener, event);}} @SuppressWarnings ({"Unchecked", "rawtypes"}) private void Doinvokelistener (Applicationlistener listener, Applicationevent event) {try {listener.onapplicationevent (event);} catch (ClassCastException ex) {String msg = Ex.getmessage (); if (msg = = NULL | | matchesclasscastmessage (MSG, Event.getclass (). GetName ()) {//possibly a lambda-defined listener which we could not resolve the generic event type for//-let ' s Suppress the exception and just log a debug message. Log logger = Logfactory.getlog (GetClass ()), if (logger.isdebugenabled ()) {Logger.debug ("non-matching Event type for Listener: "+ Listener, Ex);}} else {throw ex;}}}
Custom Events Demo
Package Com.spring.event.event;import Org.springframework.context.applicationevent;import com.spring.event.bean.notify;/** * Custom Event * @author SXQ * @time August 31, 2018 Morning 10:26:24 * */public class Notifyevent extends applicationevent {private int version;private Notify notify;public int getversion () {return version;} public void setversion (int version) {this.version = version;} /** * Serialversionuid */private static final long serialversionuid = -6198589267233914254l;public notifyevent (Object sou RCE) {super (source);} Public Notify getnotify () {return Notify;} public void Setnotify (Notify Notify) {this.notify = Notify;} Public notifyevent (Object source,notify Notify) {this (source); this.notify = Notify;}}
Package Com.spring.event.listner;import Org.springframework.context.applicationlistener;import Org.springframework.stereotype.component;import com.spring.event.event.notifyevent;/** * Custom Listener * @author SXQ * @time August 31, 2018 Morning 10:27:04 * */@Componentpublic class Notifylistenr implements applicationlistener<notifyevent>{@ overridepublic void Onapplicationevent (Notifyevent event) {System.out.println (Event.getnotify (). toString ());// After listening for events, handling subsequent things}}
ApplicationContext context = new Classpathxmlapplicationcontext ("Spring.xml"); Notify Notify = new Notify ("John Doe", 21); Notifyevent event = new Notifyevent ("Notifyevent", notify); Event.setversion (100);//Publish Events Context.publishevent (event);
Spring event Monitoring Mechanism