ApplicationContext support for events in spring

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.