spring监听器demo

欢迎进入《一起学spring》系列博文第三篇,

spring容器的事件监听机制,同样有事件、事件源和监听者。而spring中的事件需要继承ApplicationEvent,监听者需要继承ApplicationListener。其他的基本和普通的事件监听差不多。我们用示例说话!


1、这是spring的ApplicationEvent类的源码,我们可以看到它继承了JDK中的EventObject,EventObject中只有一个Object类型的source属性以及一个getSource方法。下面这个ApplicationEvent类只是增加了一个时间戳的属性以及getTimeStamp的方法。

[java]  view plain  copy
  1. package org.springframework.context;  
  2.   
  3. import java.util.EventObject;  
  4.   
  5. /** 
  6.  * Class to be extended by all application events. Abstract as it 
  7.  * doesn't make sense for generic events to be published directly. 
  8.  * 
  9.  * @author Rod Johnson 
  10.  * @author Juergen Hoeller 
  11.  */  
  12. public abstract class ApplicationEvent extends EventObject {  
  13.   
  14.     /** use serialVersionUID from Spring 1.2 for interoperability */  
  15.     private static final long serialVersionUID = 7099057708183571937L;  
  16.   
  17.     /** System time when the event happened */  
  18.     private final long timestamp;  
  19.   
  20.   
  21.     /** 
  22.      * Create a new ApplicationEvent. 
  23.      * @param source the component that published the event (never {@code null}) 
  24.      */  
  25.     public ApplicationEvent(Object source) {  
  26.         super(source);  
  27.         this.timestamp = System.currentTimeMillis();  
  28.     }  
  29.   
  30.   
  31.     /** 
  32.      * Return the system time in milliseconds when the event happened. 
  33.      */  
  34.     public final long getTimestamp() {  
  35.         return this.timestamp;  
  36.     }  
  37.   
  38. }  


2、spring中的ApplicationListener源码;我们可以清楚地知道,这个类和我们自定义监听器中的监听器类基本一样。
[java]  view plain  copy
  1. public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {  
  2.   
  3.     /** 
  4.      * Handle an application event. 
  5.      * @param event the event to respond to 
  6.      */  
  7.     void onApplicationEvent(E event);  
  8.   
  9. }  

下面回到我们的代码:

3、我的事件

[java]  view plain  copy
  1. package com.huai.listener;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4.   
  5. public class MyEvent extends ApplicationEvent{  
  6.   
  7.     private String text;  
  8.       
  9.     public MyEvent(Object source) {  
  10.         super(source);  
  11.     }  
  12.       
  13.     public void setText(String text){  
  14.         this.text = text;  
  15.     }  
  16.       
  17.     public String getText(){  
  18.         return this.text;  
  19.     }  
  20.   
  21. }  

4、我的监听器

[java]  view plain  copy
  1. package com.huai.listener;  
  2.   
  3. import org.springframework.context.ApplicationEvent;  
  4. import org.springframework.context.ApplicationListener;  
  5.   
  6. public class MyListener implements ApplicationListener<ApplicationEvent>{  
  7.   
  8.     @Override  
  9.     public void onApplicationEvent(ApplicationEvent event) {  
  10.         if(event instanceof MyEvent){  
  11.             System.out.println("my event laungh");  
  12.         }else{  
  13.             System.out.println("other envet");  
  14.         }  
  15.     }  
  16.   
  17. }  

5、测试类:

[java]  view plain  copy
  1. package com.huai.listener;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. import org.springframework.context.*;  
  5.   
  6. public class SpringTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  10.         MyEvent event = new MyEvent("hello World");  
  11.         event.setText("hello");  
  12.           
  13.         context.publishEvent(event);  
  14.     }  
  15. }  
仔细看上面的测试类,MyEvent类已经实例化,但MyListener类呢?我们并没有实例化,那么我们应该让容器实例化,所以我们需要在spring的配置文件中告诉spring容器,让它帮我们实例化MyListener类。我们还应该注意到这条代码:context.publishEvent(event);作用是让spring容器中的所有监听器都知道有这样一个事件发生了。在spring源码中的解释是:Notify all listeners registered with this application of an application event. Events may be framework events (such as RequestHandledEvent) or application-specific events.


6、spring的配置文件:我们在spring中配置了一个实现了ApplicationListener的Bean,Spring容器就会把这个bean当成容器事件的监听器。

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.            http://www.springframework.org/schema/context    
  9.            http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  10.            http://www.springframework.org/schema/aop    
  11.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  12.            http://www.springframework.org/schema/tx     
  13.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  14.            http://www.springframework.org/schema/mvc  
  15.            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  16.   
  17.   
  18.     <bean class="com.huai.listener.MyListener"/>  
  19.   
  20. </beans>  


运行结果为:

other envet
my event laungh


第一个是spring容器内置的事件;

第二个是我们自己的事件。

实际上,如果开发者需要在spring容器中初始化、销毁时回调自定义的方法,就可以通过上面的事件监听器来实现。

猜你喜欢

转载自blog.csdn.net/magicianjun/article/details/78843905