spring-第三篇之ApplicationContext的事件機制


1、通過ApplicationEvent類和ApplicationListener接口,可以實現ApplicationContext的事件處理。

     如果容器中有一個ApplicationListener bean,當ApplicationContext發布ApplicationEvent時,ApplicationListener bean將自動被觸發。

2、spring的事件框架的兩個重要成員(即Event、Listener):

     1》ApplicationEvent:容器事件,必須由ApplicationContext發布。

     2》ApplicationListener:監聽器,可由容器中的任何監聽器bean擔任。

     事件機制中的3要素:事件源(ApplicationContext)、事件(Event)、事件監聽器(Listener)。

     Event事件——>ApplicationContext事件源發布事件——>觸發Listener監聽器——>監聽器執行內部onApplicationEvent(ApplicationEvent e)方法,Event事件作為傳入參數。

     由上面的流程可知,只需要在容器中注冊實現了ApplicationListener的bean,當ApplicationContext發布事件的時候,就會被監聽器自動捕獲了。

     beans.xml注冊事件監聽器實示例:

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring配置文件的根元素,使用spring-beans-4.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
      <!-- 配置監聽器 -->
      <bean class="com.lfy.listener.EmailNotifier"/>
</beans>

     實現ApplicationListener接口的簡單EmailNotifier.java示例代碼如下:

package com.lfy.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import com.lfy.event.EmailEvent;

/**
 * 容器事件的監聽器類
 * @author lfy
 *
 */
public class EmailNotifier implements ApplicationListener {
    //該方法會在容器發生事件時自動觸發
    @Override
    public void onApplicationEvent(ApplicationEvent evt) {
        //只處理EmailEvent,模擬發送email通知
        if(evt instanceof EmailEvent) {
            EmailEvent emailEvent=(EmailEvent)evt;
            System.out.println("需要發送郵件的接收地址 "+emailEvent.getAddress());
            System.out.println("需要發送郵件的郵件正文 "+emailEvent.getText());
        }else {
            //其他事件不做任何處理
            System.out.println("其他事件:"+evt);
        }
    }
}

    繼承ApplicationEvent的簡單事件java bean舉例EmailEvent.java

package com.lfy.event;

import org.springframework.context.ApplicationEvent;

/**
 * ApplicationContext的事件機制
 * 只要一個java類繼承了ApplicationEvent基類,則該類的對象
 * 就可以作為spring容器的容器事件
 * @author lfy
 * 
 */
public class EmailEvent extends ApplicationEvent {
   private String address;
   private String text;
   
   public String getAddress() {
       return address;
   }
   public void setAddress(String address) {
       this.address = address;
   }
   public String getText() {
       return text;
   }
   public void setText(String text) {
       this.text = text;
   }
   
   public EmailEvent(Object source) {
       super(source);
   }
   //初始化全部成員變量的構造器
   public EmailEvent(Object source,String address,String text) {
       super(source);
       this.address=address;
       this.text=text;
   }
   
}

     簡單的ApplicationContext事件源演示,用於發布事件,觸發監聽器執行處理:

     SpringListenerTest.java

package com.lfy.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lfy.event.EmailEvent;

public class SpringListenerTest {

    public static void main(String[] args) {
        //創建spring容器
        ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
        EmailEvent ele=new EmailEvent("test","spring_test@163.com","this is a test");
        //發布容器事件
        ctx.publishEvent(ele);
    }

}

    執行結果:

   注:上面運行結果中的“其他事件”,是因為當系統創建spring容器、加載spring容器、容器銷毀時會自動觸發容器事件(內置事件),容器事件監聽器可以監聽到這些事件。ApplicationContext的publishEvent(...)用於主動觸發指定Event事件的容器事件。

   如果想讓bean中業務過程發布指定容器事件,則應該先讓bean獲得ApplicationContext容器的引用,然后將指定容器事件Event交由ApplicationContext發布。spring-讓bean獲取所在的容器

3、spring的提供的內置事件:

     1》ContextRefreshedEvent:ApplicationContext容器初始化或刷新觸發該事件。此處說的初始化,是指所有的bean被成功加載,后處理的bean被檢測激活,所有的singleton bean被預初始化,ApplicationContext容器已就緒可用。

     2》ContextStartdEvent:當使用ApplicationContext的子接口ConfigurableApplicationContex接口的start()方法啟動ApplicationContext容器時觸發該事件。容器管理生命周期的bean實例將獲得一個指定的啟動信號,這在經常需要停止后重新啟動的場合比較常見。

     3》ContextClossedEvent:當使用ConfigurableApplicationContex接口的close()方法關閉ApplicationContext容器時觸發該事件。

     4》ContextStoppedEvent:當使用ConfigurableApplicationContex接口的stop()方法使ApplicationContext容器停止時觸發該事件 。此處的“停止”意味着,容器管理生命周期的bean實例將獲得一個指定的停止信號。被停止的spring容器可以再次通過調用start()方法重新啟動。

     5》RequestHandledEvent:Web相關的事件,只能應用於使用DispatcherServlet的Web應用中。在使用spring作為前端的MVC控制器時,當spring處理用戶請求結束后,系統會自動觸發該事件。


免責聲明!

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



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