Spring's Listener Events Applicationlistener and applicationevent usage

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 oberver design pattern.

Here's an example:

First create a Applicationevent implementation class:

  1. Package com.spring.event;
  2. Import org.springframework.context.ApplicationEvent;
  3. /**
  4. * <p>class:emaillistevent</P>
  5. * <p>description:</P>
  6. * @author Liam
  7. * @Date [2012-9-7-8:42:07]
  8. */
  9. public class EmailEvent extends Applicationevent {
  10. /**
  11. * <p>description:</P>
  12. */
  13. Private static final long serialversionuid = 1L;
  14. public String address;
  15. public String text;
  16. Public emailevent (Object source) {
  17. Super (source);
  18. }
  19. Public EmailEvent (Object source, string address, string text) {
  20. Super (source);
  21. this.address = address;
  22. this.text = text;
  23. }
  24. public void print () {
  25. System.out.println ("Hello Spring event!");
  26. }
  27. }

Give the Listener:

  1. Package com.spring.event;
  2. Import org.springframework.context.ApplicationEvent;
  3. Import Org.springframework.context.ApplicationListener;
  4. /**
  5. * <p>class:emaillistener</P>
  6. * <p>description:</P>
  7. * @author Liam
  8. * @Date [2012-9-7-8:44:53]
  9. */
  10. public class Emaillistener implements Applicationlistener {
  11. public void Onapplicationevent (Applicationevent event) {
  12. if (event instanceof EmailEvent) {
  13. EmailEvent emailevent = (emailevent) event;
  14. Emailevent.print ();
  15. System.out.println ("The Source is:" +emailevent.getsource ());
  16. System.out.println ("The address is:" +emailevent.address);
  17. SYSTEM.OUT.PRINTLN ("The email ' s context is:" +emailevent.text);
  18. }
  19. }
  20. }

Applicationcontext.xml File configuration:

<bean id= "Emaillistener" class= "Com.spring.event.EmailListener" ></bean>

Test class:

  1. Package com.spring.event;
  2. Import Org.springframework.context.ApplicationContext;
  3. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  4. /**
  5. * <p>class:test</P>
  6. * <p>description:</P>
  7. * @author Liam
  8. * @Date [2012-9-6-10:41:34]
  9. */
  10. public class Test {
  11. public static void Main (string[] args) {
  12. ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
  13. Hellobean Hello = (hellobean) context.getbean ("Hellobean");
  14. Hello.setapplicationcontext (context);
  15. EmailEvent event = new EmailEvent ("Hello", "[email protected]", "This is a email text!");
  16. Context.publishevent (event);
  17. System.out.println ();
  18. }
  19. }
    1. Test results:
      1. <pre class=" html "  name= "code" >hello spring  event!  
      2. The source is:hello   
      3. THE&NBSP;ADDRESS&NBSP;IS:[EMAIL&NBSP;PROTECTED]&NBSP;&NBSP;
      4. the  Email ' s context is:this is a email text!  
      5. </pre>  
      6. span class= "tag" ><pre></ pre> 

Spring's Listening events Applicationlistener and applicationevent usage

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.