Spring Boot: General injection Instructions (v) @Component, application event events provide support for communication between beans and beans

Source: Internet
Author: User

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

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.