Event handling of Spring (ix)

Source: Internet
Author: User
Tags getmessage tutorialspoint

The core of spring is ApplicationContext , which manages the full life cycle of the bean. ApplicationContext publishes certain types of events when the bean is loaded. For example,contextstartedevent When the context starts and publishes contextstoppedevent When the context stops publishing.

ApplicationContext the event handling in the applicationevent Classes and Applicationlistener interface provided by the. Therefore, if the bean implements the applicationlistener, then each time it applicationevent The Bean is notified when it is published to ApplicationContext.

Spring's event handling is single-threaded, so if the event is published, the process will be blocked unless all recipients receive the message, and the process will not continue. Therefore, if you want to use event handling, you should be careful when designing your application.

Listening for context events

to listen for context events, the bean should implement Applicationlistener Interface, the interface has only one onapplicationevent () method . So let's write an example to see how events propagate and how to make code perform the required tasks based on specific events.

Example:

(1) Writing Helloworld.java

 Package Com.tutorialspoint;  Public class HelloWorld {   private  String message;     Public void setmessage (String message) {      this.message  = message;   }     Public void GetMessage () {      System.out.println ("Your message:" + message);}   }

(2) Writing Cstarteventhandler.java

 Package Com.tutorialspoint; Import Org.springframework.context.ApplicationListener; Import org.springframework.context.event.ContextStartedEvent;  Public class Cstarteventhandler     Implements Applicationlistener<contextstartedevent>{   publicvoid  Onapplicationevent (Contextstartedevent event) {      System.out.println ("Contextstartedevent Received"  );   }}

(3) Writing Cstopeventhandler.java

 Package Com.tutorialspoint; Import Org.springframework.context.ApplicationListener; Import org.springframework.context.event.ContextStoppedEvent;  Public class Cstopeventhandler     Implements Applicationlistener<contextstoppedevent>{   publicvoid  Onapplicationevent (Contextstoppedevent event) {      System.out.println ("Contextstoppedevent Received"  );   }}

(4) Writing Mainapp.java

 PackageCom.tutorialspoint;ImportOrg.springframework.context.ConfigurableApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {Configurableapplicationcontext context=NewClasspathxmlapplicationcontext ("Beans.xml"); //Let us raise a start event.Context.start (); HelloWorld obj= (HelloWorld) context.getbean ("HelloWorld");      Obj.getmessage (); //Let us raise a stop event.Context.stop (); }}

(5) Writing Beans.xml

<?xml Version = "1.0" encoding = "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-4.0.xsd "><bean id = "HelloWorld"class= "Com.tutorialspoint.HelloWorld" > <property name = "message" VALUE = "Hello world!" /> </bean> <bean id = "Cstarteventhandler"class= "Com.tutorialspoint.CStartEventHandler"/> <bean id = "Cstopeventhandler"class= "Com.tutorialspoint.CStopEventHandler"/> </beans>

(6) Run the Main method in Mainapp.java, the result is as follows:

Event handling of Spring (ix)

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.