ApplicationContext has the ability to publish events. This is because the interface inherits the Applicationeventpublisher interface. The event-related interfaces and classes in spring mainly include Applicationevent, Applicationlistener.
The class that defines an event needs to inherit the Applicationevent or Applicationcontextevent abstract class, which has only one constructor and a parameter of type object as the event source, and the event source cannot be null. So we need to execute super (Object) in our own constructor.
public class Userevent extends applicationevent{ private String eventcontent; Public String geteventcontent () { return eventcontent; } public void Seteventcontent (String eventcontent) { this.eventcontent = eventcontent; } Public userevent (Object source,string eventcontent) { super (source); This.eventcontent = Eventcontent; }}
For an event, a specific listener may be required, so the listener needs to implement the Applicationlistener interface. When the listener receives an event, it executes its onapplicationevent () method. Because the event model in spring IOC is a simple, coarse-grained listening model, when an event arrives, all listeners receive and respond, and if you want to listen only to certain types, you need to control it in your code.
public class Userlistener implements applicationlistener{public void Onapplicationevent (Applicationevent event) { if (event instanceof userevent) {//Only userevent type is processed userevent UE = (userevent) event; String result = Ue.geteventcontent (); System.out.println ("Event Content:" +result);}}}
For publishing events, we can implement Applicationcontextaware or Applicationeventpublisheraware interfaces.
public class Userbiz implements Applicationcontextaware{private applicationcontext applicationcontext;public void Setapplicationcontext (ApplicationContext applicationcontext) throws beansexception{ This.applicationcontext = ApplicationContext; }public void Service (String thing) { userevent event = new Userevent (this,thing); Event.seteventcontent ("I shoud" +thing); Applicationcontext.publishevent (event);} }
Or as follows:
public class USERBIZ2 implements applicationeventpublisheraware{ private Applicationeventpublisher Applicationeventpublisher; public void Setapplicationeventpublisher (Applicationeventpublisher applicationeventpublisher) { This.applicationeventpublisher = Applicationeventpublisher; } public void Service (String thing) { Userevent event = new Userevent (this,thing); Event.seteventcontent ("I shoud" +thing); Applicationeventpublisher.publishevent (event); }}
This completes the release of the event, and when the event is received by ApplicationContext, the broadcast of the event is made within spring, without needing to know the specifics. In fact, after spring reads the configuration file, it uses reflection to find all implementations of the Applicationlistener bean and register it as the container's event listener. When an event is received, spring invokes the event listener one at a time. All that's left to do is configure the listener in the configuration file.
<bean class= "Footprint.spring.ioc.event.UserListener"/>
The spring container itself publishes events, including Contextclosedevent, Contextrefreshedevent, Contextstartedevent, and Contextstoppedevent.
ApplicationContext support for events in spring