Spring Custom event Publishing and monitoring (simple instance)

Source: Internet
Author: User
Tags event listener

Objective:

Spring's Appilcaitioncontext is able to publish events and register the corresponding event listener, so it has a complete set of event publishing and monitoring mechanisms.

Process Analysis:

In a complete event system, in addition to events and listeners , there should be 3 concepts;

1. Event Source : The event's creator, any event must have a source of events;

2. event Broadcaster : It is a bridge between events and event listeners, responsible for notifying events to the event listener;

3. Event Listener Registry : The Spring Framework provides a place for all listeners to store;

Through the flowchart, you can see how they do their own work, as follows:

In fact, through the flowchart, we can easily find that the event system is the specific implementation of the observer pattern, it does not have any mystery.

Structural Analysis:

1. event class (applicaitonevent): Currently the spring framework itself provides only a few events, and many events need to be customized.

The Applicationevent Unique constructor is applicaitonevent (Object source), which specifies the source of the event through source. It has two sub-classes;

(1) Applicationcontextevent: Container event, that is, the event source is ApplicationContext, the framework provides four subclasses, each representing the container start, refresh, stop and close events.

(2) Requesthandleevent: This is a Web application-related event that occurs when a request is processed.

In general, we are all extended applicationevent to customize events. There will be chestnuts below.

2. Event Listener Interface (applicationlistener)

All listeners need to implement the interface, which defines only one method: Onapplicaitonevent (E event), which receives the event object and writes the response processing logic for the event in the method.

Example Analysis:

The process of event publishing and event monitoring is explained through an example. This example includes a simulated mail sender, MailSender, which generates an Mailsendevent event when sending mail to the destination, and we register the Listener Mailsendlistener for the event in the container;

On code: Mailsendevent

/*** Inherit the applicationcontextevent, it's a container event*/ Public classMailsendeventextendsapplicationcontextevent {PrivateString to;//Destination     Publicmailsendevent (ApplicationContext source, String to) {Super(source);  This. to =to ; }     PublicString Getto () {return  This. to; }}

Obviously, the source here is the ApplicationContext container.

We'll define a listener for this event Mailsendlistener, as follows

@Component  Public class Implements Applicationlistener<mailsendevent>{    @Override    publicvoid  Onapplicationevent (mailsendevent mailsendevent) {        = mailsendevent;        System.out.println ("MailSender" + event.getto () + "sent Mail");}    }

As mentioned above, all listeners must implement Applicationlistener, which is a generic class that specifies that the listener listens only to mailsendevent events.

OK, let's now trigger the event: MailSender, which is the event source

@Component ("MailSender")publicclass  mailsender {    @Autowired      Private ApplicationContext ApplicationContext;  // Container event triggered    by container  Public void SendMail (String to) {        System.out.println ("MailSender start sending mail");         New mailsendevent (applicationcontext,to);        Applicationcontext.publishevent (event);    }}

Finally write a test class, the container starts:

 Public class springeventtest {    publicstaticvoid  main (string[] args) {          New Classpathxmlapplicationcontext ("Web/web-inf/applicationcontext.xml");        MailSender Sender  = (mailsender) context.getbean ("MailSender");        Sender.sendmail ("Beijing");}    }

Remember in applicationcontext.xml inside add annotation scan ah, I here are put in springevent bag, so add <context:component-scan base-package= " Springevent "/>

OK, start the container, send the message: execution results are as follows

Spring Custom Event Publishing and listening (simple instance)

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.