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)