欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

spring中怎么監(jiān)聽ApplicationEvent事件現

這篇文章將為大家詳細講解有關spring中怎么監(jiān)聽ApplicationEvent事件現,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創(chuàng)新互聯公司專注于昌吉網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供昌吉營銷型網站建設,昌吉網站制作、昌吉網頁設計、昌吉網站官網定制、微信小程序開發(fā)服務,打造昌吉網絡公司原創(chuàng)品牌,更為您提供昌吉網站排名全網營銷落地服務。

原理:ApplicationContextAware接口提供了publishEvent方法,實現了Observe(觀察者)設計模式的傳播機制,實現了對bean的傳播。通過ApplicationContextAware我們可以把系統(tǒng)中所有ApplicationEvent傳播給系統(tǒng)中所有的ApplicationListener。

1、直接上代碼
2、定義自己的監(jiān)聽器(負責處理自己的監(jiān)聽事件)
3、定義一個bean觸發(fā)監(jiān)聽事件
4、測試
package com.test.eventListener;

import org.springframework.context.ApplicationEvent;

/**
 * [@author](https://my.oschina.net/arthor) admin
 * [@date](https://my.oschina.net/u/2504391) 2018/5/17 17:37
 * 新建StudentAddEvent類,實現抽象類org.springframework.context.ApplicationEvent
 * StudentAddEvent類中需要實現自己的構造函數
 *  增加學生監(jiān)聽事件
 */
public class StudentAddEvent extends ApplicationEvent {

    private static final long serialVersionUID = 20L;

    /**
     * 學生姓名
     */
    private String name;

    /**
     * [@param](https://my.oschina.net/u/2303379) source
     */
    public StudentAddEvent(Object source, String name) {
        super(source);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.test.eventListener;

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

/**
 * [@author](https://my.oschina.net/arthor) admin
 * 新建StudentAddListener類,實現接口org.springframework.context.ApplicationListener中的onApplicationEvent方法,
 * 在該方法中只處理StudentAddEvent類型的ApplicationEvent事件
 * 定義StudentAddListener監(jiān)聽器
 */
[@Component](https://my.oschina.net/u/3907912)
public class StudentAddListener implements ApplicationListener {

    public void onApplicationEvent(ApplicationEvent event) {
        // 1.判斷是否是增加學生對象的事件
        if (!(event instanceof StudentAddEvent)) {
            return;
        }

        // 2.是增加學生事件的對象,進行邏輯處理,比如記日志、積分等
        StudentAddEvent studentAddEvent = (StudentAddEvent) event;
        System.out.println("增加了學生:" + studentAddEvent.getName());
    }
}
package com.test.eventListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author admin
 * 定義StudentAddBean觸發(fā)StudentAddEvent事件
 * 新建StudentAddBean類,實現接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法,
 * 在構造bean的時候注入Spring的上下文對象,以便通過Spring上下文對象的publishEvent方法來觸發(fā)StudentAddEvent事件
 */
@Component
public class StudentAddBean implements ApplicationContextAware {
    /**
     * 定義Spring上下文對象
     */
    private ApplicationContext applicationContext = null;

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

    }

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

}
package com.test.eventListener;

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

/**
 * @author admin
 * ApplicationContext在運行期會自動檢測到所有實現了ApplicationListener的bean對象,并將其作為事件接收對象。
 * 當ApplicationContext的publishEvent方法被觸發(fā)時,每個實現了ApplicationListener接口的bean都會收到ApplicationEvent對象,
 * 每個ApplicationListener可根據事件類型只接收處理自己感興趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
 */
public class EventListenerTest {
    public static void main(String[] args) {
        String[] xmlConfig = new String[] { "spring/spring.xml" };
        // 使用ApplicationContext來初始化系統(tǒng)
        ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig);
        StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean");
        studentBean.addStudent("張三");
        studentBean.addStudent("李四");
    }
}

關于spring中怎么監(jiān)聽ApplicationEvent事件現就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

文章標題:spring中怎么監(jiān)聽ApplicationEvent事件現
本文來源:http://chinadenli.net/article34/jpeose.html

成都網站建設公司_創(chuàng)新互聯,為您提供服務器托管網頁設計公司網站設計公司域名注冊響應式網站網站營銷

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

外貿網站制作