Tips for event handling in spring

Source: Internet
Author: User
Tags getmessage stub

Spring provides some aware-related interfaces, Beanfactoryaware, Applicationcontextaware, Resourceloaderaware, Servletcontextaware, and so on, One of the most commonly used is applicationcontextaware. The bean that implements the Applicationcontextaware, after the bean is initialized, will be injected into the ApplicationContext instance. Applicationcontextaware provides the Publishevent () method, which implements the event PROPAGATOR for Observer (Observer) design patterns, and provides event propagation for the bean. With the Application.publishevent method, we can notify all applicationlistener in the system of the event.

Spring Event Handling General procedure:

• Define the event class, inheriting org.springframework.context.ApplicationEvent.

• Write Publishing event class Publisher to implement the Org.springframework.context.ApplicationContextAware interface.

• Overlay Method Setapplicationcontext (ApplicationContext ApplicationContext) and publish method publish (Object obj)

• Define time Listening class EventListener, implement Applicationlistener interface, implement method Onapplicationevent (Applicationevent event).

Java code

import org.springframework.context.ApplicationEvent;
/**
* 定义事件信息
* @author new
*
*/
public class MessageEvent extends ApplicationEvent {
  private String message;
  public void setMessage(String message){
   this.message = message;
  }
  public String getMessage(){
   return message;
  }
  public MessageEvent(Object source, String message) {
   super(source);
   this.message = message;
   // TODO Auto-generated constructor stub
  }
  private static final long serialVersionUID = 1L;
}

Java code

import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;  
public class Publisher implements Applicationcontextaware {
Private applicationcontext context;
@Override
public void Setapplicationcontext (ApplicationContext arg0)
throws beansexception {
//TODO Au   to-generated method Stub
This.context = arg0;
 
public void Publish (String message) {
Context.publishevent (new Messageevent (This,message));

public static void Main (string[] args) {
ApplicationContext ctx = new Filesystemxmlapplicationcontext ("src/ Applicationcontext.xml ");
Publisher pub = (publisher) Ctx.getbean ("publisher");
Pub.publish ("Hello world!");
Pub.publish ("The quick brown fox jumped over the lazy dog");
}
}
/p>

Java code

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class MessageEventListener implements ApplicationListener {
  @Override
  public void onApplicationEvent(ApplicationEvent event) {
   // TODO Auto-generated method stub
   if(event instanceof MessageEvent){
    MessageEvent msEvent = (MessageEvent)event;
    System.out.println("Received: " + msEvent.getMessage());
   }
  }
}

At run time, ApplicationContext automatically looks for an implementation of the Applicationlistener interface in all current beans and takes it as an event-receiving object. When the Application.publishevent method is invoked, all Applicationlistener interface implementations are fired, and each applicationlistener can be judged by the type of event as the event it needs to handle, as above actionlist Ener only handles ActionEvent events.

Related Article

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.