Spring's applicationevent and Applicationlistener

Source: Internet
Author: User

What is ApplicationContext?
It is the core of spring, which we usually interpret as context, but it is better to understand it as a container.
The ApplicationContext is the container for the application.

Spring puts the bean (object) in the container and needs to be taken out by the Get method.

Applicationevent

is an abstract class in which there is only one constructor and one long integer timestamp.

Applicationlistener

is an interface in which there is only one onapplicationevent method.

So your class will implement the interface when it implements the method.

If you deploy a bean that implements the Applicationlistener interface in context,

So whenever a applicationevent is released to ApplicationContext,
This bean is notified. In fact, this is the standard observer design pattern.

First, create a class of event events:

   1. Public class Emaillistevent extends Applicationevent {     2.      3.     private static final long serialversionuid = 1L;     4. Public     String address;     5. Public     String text;     6.      7.     Public emaillistevent (Object source) {     8.         Super (source);     9.    Ten.     Public     emaillistevent (Object source, string address, string text) {    .         Super (source);         this.address = address;         this.text = text;    .     . public     void Print () {    .         System.out.println ("hello,spring Event!!!");    20.}  

Next, create a Applicationlistener class:

   1. Public class Emailnotifier implements Applicationlistener {     2.      3. Public     void Onapplicationevent (Applicationevent evt) {     4.         if (evt instanceof emaillistevent) {     5.             Emaillistevent emailevent = (emaillistevent) evt;     6.             emailevent.print ();     7.             System.out.println ("The source is:" + emailevent.getsource ());     8.             System.out.println ("The address is:" + emailevent.address);     9.             System.out.println ("The mail ' s context is:" + emailevent.text);    Ten.         }    .    13.}   

The listener is then registered in the Spring XML file:

<?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:aop= "http://www.springframework.org/ SCHEMA/AOP "            xmlns:tx=" http://www.springframework.org/schema/tx "            xsi:schemalocation="/http/ Www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd            HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd            http://www.springframework.org/schema/tx           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd " >         <bean id= "Emaillistlistener" class= "Spring.event.EmailNotifier" ></bean>        </beans >  

Finally, create the demo class:

   1. public class Listenereventdemo {     2.      3.     /**    4.      * @param args    5.      */     6.     public static void Main (string[] args) {     7.         ApplicationContext context = new Classpathxmlapplicationcontext (     8.                 ") Springevent.xml ");     9.         emaillistevent emaillistevent = new Emaillistevent ("Hello", "    Ten."                 ) [Email protected] "," This is a test eamil content ");    One.         //Publish a applicationevent in ApplicationContext    .         Context.publishevent (emaillistevent);    .     15.}  

Test results:

# hello,spring Event!!!  # The source Is:hello  # The address Is:[email protected]  # The Mail ' s context is:this is a test eamil content  



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.