Spring Events (Application event)

Source: Internet
Author: User

The spring event passes a message between the bean and the bean. One bean is finished. Hopefully the other one goes ahead. Then we need the rest of the beans to listen for the events sent by the current bean.

The following steps are used for spring events:

1. Customize the event first: your event needs to inherit Applicationevent

2. Define the event listener: need to implement Applicationlistener

3. Using containers to publish events

The following example is a scenario where a scene is sent when the message is registered:

Define the event first:

 Packagecom.foreveross.service.weixin.test.springevent;Importorg.springframework.context.ApplicationEvent;/*** Customize an event *@authorMingge **/ Public classDemoeventextendsapplicationevent{PrivateString msg; PrivateString Email;  Publicdemoevent (Object source,string msg,string email) {Super(source);  This. msg=msg;  This. email=email; }     PublicString getmsg () {returnmsg; }     Public voidsetmsg (String msg) { This. msg =msg; }     PublicString Getemail () {returnemail; }     Public voidsetemail (String email) { This. email =email; }    }

Then define the event listener:

 Packagecom.foreveross.service.weixin.test.springevent;ImportOrg.springframework.context.ApplicationListener;ImportOrg.springframework.scheduling.annotation.Async;Importorg.springframework.stereotype.Component;/*** Define an Event listener class *@authorMingge **/@Component Public classDemoeventlistenerImplementsApplicationlistener<demoevent>{    //the use of annotations @async support so that not only can support through calls, but also support asynchronous calls, very flexible,@Async @Override Public voidonapplicationevent (Demoevent event) {System.out.println ("Registration successful, send confirmation email:" + event.getemail () + ", Message digest:" +event.getmsg ()); }}

To publish the event again using the container:

 Packagecom.foreveross.service.weixin.test.springevent;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.context.ApplicationContext;Importorg.springframework.stereotype.Component;/*** Event Publishing class *@authorMingge **/@Component Public classDemoeventpublisher {@AutowiredPrivateApplicationContext ApplicationContext;  Public voidpushlish (String msg,string Mail) {applicationcontext.publishevent (NewDemoevent ( This, Msg,mail)); }    }

The final test is:

 Package com.foreveross.service.weixin.test.springevent; Import Org.springframework.context.annotation.ComponentScan; Import org.springframework.context.annotation.Configuration, @Configuration @componentscan (" Com.foreveross.service.weixin.test.springevent ")publicclass  eventconfig {}
 Packagecom.foreveross.service.weixin.test.springevent;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Event Test class *@authorMingge **/ Public classEventtest { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (EventConfig.class); Demoeventpublisher Demoeventpublisher=context.getbean (Demoeventpublisher.class); Demoeventpublisher.pushlish ("Zhang 31", "[email protected]"); Demoeventpublisher.pushlish ("Zhang 32", "[email protected]"); Demoeventpublisher.pushlish ("Zhang 33", "[email protected]"); Demoeventpublisher.pushlish ("Zhang 34", "[email protected]"); Demoeventpublisher.pushlish ("Zhang 35", "[email protected]");    Context.close (); }}

Reference:http://blog.csdn.net/it_man/article/details/8440737

Spring Events (Application event)

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.