1. 程式人生 > >spring 中 applicationEvent的使用 廣播

spring 中 applicationEvent的使用 廣播

Spring的ApplicationEvent的使用

Spring 3.0中提供了很多類似*Aware的類,其中ApplicationContextAware介面可以實現我們在初始化bean的時候給bean注入ApplicationConxt(Spring上下文物件)物件。ApplicationContextAware介面提供了publishEvent方法,實現了Observe(觀察者)設計模式的傳播機制,實現了對bean的傳播。通過ApplicationContextAware我們可以把系統中所有ApplicationEvent傳播給系統中所有的ApplicationListener。因此,我們只需要構造好我們自己的ApplicationEvent和ApplicationListener,就可以在系統中實現相應的監聽器。

下面以增加學生的示例來演示如何構造Spring的監聽器,StudentAddEvent是監聽的事件物件,StudentAddListener是事件的監聽器(負責處理接收到的監聽事件),StudentAddBean負責觸發StudentAddEvent事件。具體步驟如下:

  1. 定義StudentAddEvent監聽事件

新建StudentAddEvent類,實現抽象類

org.springframework.context.ApplicationEvent

StudentAddEvent類中需要實現自己的建構函式,具體程式碼如下:

package com.trs.spring.event;  

import
org.springframework.context.ApplicationEvent; /** * 增加學生的監聽事件 */ public class StudentAddEvent extends ApplicationEvent { /** * */ private static final long serialVersionUID = 20L; /** * 學生姓名 */ private String m_sStudentName; /** * @param
source */
public StudentAddEvent(Object source, String _sStudentName) { super(source); this.m_sStudentName = _sStudentName; } /** * 獲取學生姓名 * * @return */ public String getStudentName() { return m_sStudentName; } }
  1. 定義StudentAddListener監聽器

新建StudentAddListener類,實現介面

org.springframework.context.ApplicationListener

中的onApplicationEvent方法,在該方法中只處理StudentAddEvent型別的ApplicationEvent事件,程式碼如下:

package com.trs.spring.event;  

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

public class StudentAddListener implements ApplicationListener {  

    /* 
     * (non-Javadoc) 
     *  
     * @see 
     * org.springframework.context.ApplicationListener#onApplicationEvent(org 
     * .springframework.context.ApplicationEvent) 
     */  
    public void onApplicationEvent(ApplicationEvent _event) {  
        // 1.判斷是否是增加學生物件的事件   
        if (!(_event instanceof StudentAddEvent)) {  
            return;  
        }  

        // 2.是增加學生事件的物件,進行邏輯處理,比如記日誌、積分等  
        StudentAddEvent studentAddEvent = (StudentAddEvent) _event;  
        System.out.println("增加了學生:::" + studentAddEvent.getStudentName());  
    }  

}  
  1. 定義StudentAddBean觸發StudentAddEvent事件

新建StudentAddBean類,實現介面

org.springframework.context.ApplicationContextAware

中的setApplicationContext方法,在構造bean的時候注入Spring的上下文物件,以便通過Spring上下文物件的publishEvent方法來觸發StudentAddEvent事件,具體程式碼如下:

package com.trs.spring.event;  

import org.springframework.beans.BeansException;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.ApplicationContextAware;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  

public class StudentAddBean implements ApplicationContextAware {  
    /** 
     * 定義Spring上下文物件 
     */  
    private ApplicationContext m_applicationContext = null;  

    /* 
     * (non-Javadoc) 
     *  
     * @see 
     * org.springframework.context.ApplicationContextAware#setApplicationContext 
    * (org.springframework.context.ApplicationContext) 
     */  
    public void setApplicationContext(ApplicationContext _applicationContext)  
            throws BeansException {  
       this.m_applicationContext = _applicationContext;  

    }  

    /** 
     * 增加一個學生 
     *  
     * @param _sStudentName 
     */  
    public void addStudent(String _sStudentName) {  
        // 1.構造一個增加學生的事件   
        StudentAddEvent aStudentEvent = new StudentAddEvent(  
                m_applicationContext, _sStudentName);  
        // 2.觸發增加學生事件   
        m_applicationContext.publishEvent(aStudentEvent);  
    }  

    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        String[] xmlConfig = new String[] { "applicationContext.xml" };  
        // 使用ApplicationContext來初始化系統  
        ApplicationContext context = new ClassPathXmlApplicationContext(  
                xmlConfig);  
        StudentAddBean studentBean = (StudentAddBean) context  
                .getBean("StudentAddBean");  
        studentBean.addStudent("我是第一個學生");  
        studentBean.addStudent("第二個學生已經新增");  

    }  

}  

4 applicationContext.xml配置檔案

<bean id="StudentAddBean" class="com.trs.spring.event.StudentAddBean"></bean>

<bean id="StudentAddListener" class="com.trs.spring.event.StudentAddListener"></bean>

5 說明

ApplicationContext在執行期會自動檢測到所有實現了ApplicationListener的bean物件,並將其作為事件接收物件。當ApplicationContext的publishEvent方法被觸發時,每個實現了ApplicationListener介面的bean都會收到ApplicationEvent物件,每個ApplicationListener可根據事件型別只接收處理自己感興趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。

6 執行StudentAddBean的main函式,結果如下:

增加了學生:::我是第一個學生

增加了學生:::第二個學生已經新增