spring之ApplicationEvent 事件驅動


什么是ApplicationContext? 
它是Spring的核心,Context我們通常解釋為上下文環境,但是理解成容器會更好些。 
ApplicationContext則是應用的容器。

Spring把Bean(object)放在容器中,需要用就通過get方法取出來。

ApplicationEvent

是個抽象類,里面只有一個構造函數和一個長整型的timestamp。

ApplicationListener

是一個接口,里面只有一個onApplicationEvent方法。

所以自己的類在實現該接口的時候,要實裝該方法。

如果在上下文中部署一個實現了ApplicationListener接口的bean,

那么每當在一個ApplicationEvent發布到 ApplicationContext時,
這個bean得到通知。其實這就是標准的Observer設計模式

首先創建一個Event事件類

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類:

 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.     }  

接着將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>  

MAIN

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.                 "helloSpring@sina.com", "this is a test eamil content");  
  11.         //在ApplicationContext中發布一個 ApplicationEvent  
  12.         context.publishEvent(emailListEvent);  
  13.     }  
  14.   

測試結果:

# Hello,Spring Event!!! # the source is:hello # the address is:helloSpring@sina.com # the mail's context is :this is a test eamil content 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM