(spring-15th Back "IOC Basics") container events

Source: Internet
Author: User
Tags event listener

Five of people subscribed to newspapers at the newspaper. Once the newspaper had a new newspaper, they were sent to the five people. In this case, the newspaper is the event, the newspaper is the broadcaster, and five subscribers are the listeners. The broadcaster receives the event, passes the event to the listener, and the listener does something about the event. Such an example belongs to the Observer pattern.

  1. Observer Pattern : Class A is responsible for receiving new information, B, C, and d have always been concerned with a. Once a class has new information, it is sent to classes B, C, D, B, C, D to receive information to make different operations. This is the Observer pattern. Specific to the code, a variable message is set in a (this is information), and when the message is changed, the Performmessage (message) method of B, C, D is called (this sends the message to the B, C, D classes and lets the B, C, D classes begin processing. Here, the B, C, and D classes are listeners (the equivalent of a listener), and A is a publisher (equivalent to a broadcaster), and a message is an event.
  2. The Observer pattern for Java : Java.util.Observer belongs to the listener interface, and writing its own listener only needs to implement this interface. Java.util.Observable is the publisher, and writing your own publisher needs to extend this class.
  3. Event System : There are several elements:
    1. Event: Like a new newspaper, something happened, and this thing is an event.
    2. Event Source: The initiator of the thing.
    3. Event broadcaster: Notifies event listeners of events. (similar to a newspaper).
    4. Event Listener: Receive events and do some work on the events. (Subscribers have different responses after reading the newspaper separately).
    5. Event Listener registry: The framework's event listeners are stored in the registration table.
  4. event System role diagram : Event source generates events, passes events to event broadcasters, The event broadcaster then passes the event to the event listener in the event listener registry.
  5. How does spring use event broadcasters to publish events ?
    1. To publish an event, you must have several elements: an event class, a broadcaster, and a listener.
    2. Java.util.EventObject is the Java event class, and spring's applicationcontextevent extends the EventObject class. The inheritance relationship is:, where Applicationevent inherits the EventObject. We write our own Spring event class and can extend the applicationevent.
    3. Java.util.EventListener is the event listener class for Java, and Spring's Applicationlistener expands the EventListener interface. You can extend the Applicationlistener to write your own listener (configured in the configuration file after writing it).
    4. Spring's event broadcaster Inheritance relationship: The event listener Registry is provided by the event broadcaster. You can implement applicationeventmulticaster to write your own broadcaster, and if you do not have your own broadcaster, spring will use the default Simpleapplicationeventmulticaster broadcaster.
    5. event class, Listener class, broadcast class all have, and then write an event source class (manufacturing event, must implement Applicationcontextaware interface, overwrite Setapplicationcontext method, To get the ApplicationContext instance. And then configure it in the configuration file, so that the entire event process works. Specific Operating Procedures:
      1. The event broadcaster is initialized when the spring container starts, and the event broadcaster provides the listener registry.
      2. Spring takes all the listeners from the configuration file and puts them in the listener registry.
      3. When the spring container starts, it runs the event source class, manufactures the event, and passes the event to the broadcaster, which then passes the event to the listener.
  6. an instance
    1. Write event class:
       public  class  Mailsendevent extends   applicationcontextevent        { private   String to;  public   Mailsendevent ( ApplicationContext source, String to) { super   (source);      public   String Getto () {return  this     .TO; }}  

       

    2. To write the event listener class:
       Public class Implements Applicationlistener<mailsendevent>{    publicvoid  onapplicationevent ( Mailsendevent event) {            = (mailsendevent) event;            System.out.println ("Mailsendlistener: to" + mse.getto () + "send out a message");}    }

    3. Write the event source class (the spring container starts and loads the class to drive the entire event delivery process) (in the following code, within Publishevent (MSE), spring delegates the Applicationeventmulticaster broadcast event to the listener):
       Public classMailSenderImplementsApplicationcontextaware {PrivateApplicationContext CTX;  Public voidSetapplicationcontext (ApplicationContext ctx)throwsbeansexception { This. CTX =CTX; }         Public voidSendMail (String to) {System.out.println ("MailSender: Simulate sending mail ..."); Mailsendevent MSE=NewMailsendevent ( This. ctx,to);    Ctx.publishevent (MSE); }}

    4. To configure the listener and event source classes in XML:
        <class= "Com.baobaotao.event.MailSendListener"/>  < id = "MailSender"  class= "Com.baobaotao.event.MailSender"    />

    5. The event source is called in the main function:
       Public Static void Main (string[] args) {        = "Com/baobaotao/event/beans.xml";         New Classpathxmlapplicationcontext (resourcefile);             = Ctx.getbean (MailSender.  Class);        Mailsender.sendmail ("test mail.") );        System.out.println ("done." );    }

(spring-15th Back "IOC Basics") container events

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.