Spring 的 ApplicationEvent and ApplicationListener

什么是ApplicationContext?
它是Spring的核心,Context我们通常解释为上下文环境,可是理解成容器会更好些。
ApplicationContext则是应用的容器。

Spring把Bean(object)放在容器中,须要用就通过get方法取出来。

ApplicationEvent

是个抽象类,里面仅仅有一个构造函数和一个长整型的timestamp。

ApplicationListener

是一个接口,里面仅仅有一个onApplicationEvent方法。

所以自己的类在实现该接口的时候,要实装该方法。

假设在上下文中部署一个实现了ApplicationListener接口的bean,

那么每当在一个ApplicationEvent公布到 ApplicationContext时,
这个bean得到通知。事实上这就是标准的Observer设计模式。

首先创建一个Event事件类:

   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.     }  
  10.   
  11.     public EmailListEvent(Object source, String address, String text) {  
  12.         super(source);  
  13.         this.address = address;  
  14.         this.text = text;  
  15.     }  
  16.   
  17.     public void print() {  
  18.         System.out.println("Hello,Spring Event!!!");  
  19.     }  
  20. }  

其次创建一个ApplicationListener类:

   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);  
  10.         }  
  11.   
  12.     }  
  13. }  

 

接着将Listener注冊到Spring的xml文件里:

<?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>  

最后创建Demo类:

   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",  
  10.                 "[email protected]", "this is a test eamil content");  
  11.         //在ApplicationContext中公布一个 ApplicationEvent  
  12.         context.publishEvent(emailListEvent);  
  13.     }  
  14.   
  15. }  

測试结果:

# Hello,Spring Event!!!  
# the source is:hello  
# the address is:[email protected]  
# the mail's context is :this is a test eamil content  



你可能感兴趣的:(application)