Spring events provide support for communication between beans and beans, and when a bean is processed, hopefully another bean knows what to do, then we let another bean listen to the events sent by the current bean.
The spring events should follow:
1. Custom events, Integration: applicationevent
2. Custom event monitoring to achieve Applicationlistener
3. Publish Events using containers
Custom events
Package Ch2.event;import org.springframework.context.applicationevent;//Custom Event public class Demoevent extends applicationevent {private static final long Serialverisionuid = 1l;private String msg;public demoevent (Object source, Stri ng msg) {super (source);//TODO auto-generated constructor stubthis.msg = msg;} Public String getmsg () {return msg;} public void Setmsg (String msg) {this.msg = msg;}}
Event Listener
Package Ch2.event;import Org.springframework.context.applicationevent;import Org.springframework.context.applicationlistener;import org.springframework.stereotype.component;//Event Listener//@ Component The normal Pojo into the spring container, which is equivalent to the <bean id= "class=" in the configuration file/>//@ Service injection: The current class is a spring-managed Bean@componentpublic class Demolistener implements applicationlistener<demoevent> {@ overridepublic void Onapplicationevent (Demoevent event) {//TODO auto-generated method stub//Accept Message string msg = Event.getms g ();//Print Message System.out.println ("I (Bean-demolistener) received a message from Bean-demopublisher:" +msg);}}
Event Send
Package Ch2.event;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import org.springframework.stereotype.component;//Event Send// Instantiate an ordinary pojo into a spring container as a bean@componentpublic class Demopublisher {//ApplicationContext entity bean into Demopublisher, Let Demopublisher have ApplicationContext features//use ApplicationContext to publish events @autowiredapplicationcontext ApplicationContext ;p ublic void Publish (String msg) {//uses ApplicationContext's event to publish the message applicationcontext.publishevent (new Demoevent ( This, msg));}}
Configuration class
Package Ch2.event;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.componentscan;//declares that this class is a configuration class @configuration//all @service,@ under the import ch2.event package Component, @Repository, @Controller registered as Bean@componentscan ("Ch2.event") public class EventConfig {}
Run:
Package Ch2.event;import Org.springframework.context.annotation.annotationconfigapplicationcontext;public Class Main {public static void main (string[] args) {Annotationconfigapplicationcontext context = new Annotationconfigapplicationcontext (eventconfig.class);D emopublisher demopublisher = Context.getBean ( Demopublisher.class);d emopublisher.publish ("Hello Application Event"); Context.close ();}}
Results:
I (Bean-demolistener) received a message from Bean-demopublisher: Hello Application Event
Spring Boot: General injection Instructions (v) @Component, application event events provide support for communication between beans and beans