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.