溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

spring中怎么監(jiān)聽(tīng)ApplicationEvent事件現(xiàn)

發(fā)布時(shí)間:2021-06-21 16:48:12 來(lái)源:億速云 閱讀:169 作者:Leah 欄目:大數(shù)據(jù)

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

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

1、直接上代碼
2、定義自己的監(jiān)聽(tīng)器(負(fù)責(zé)處理自己的監(jiān)聽(tīng)事件)
3、定義一個(gè)bean觸發(fā)監(jiān)聽(tīng)事件
4、測(cè)試
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類(lèi),實(shí)現(xiàn)抽象類(lèi)org.springframework.context.ApplicationEvent
 * StudentAddEvent類(lèi)中需要實(shí)現(xiàn)自己的構(gòu)造函數(shù)
 *  增加學(xué)生監(jiān)聽(tīng)事件
 */
public class StudentAddEvent extends ApplicationEvent {

    private static final long serialVersionUID = 20L;

    /**
     * 學(xué)生姓名
     */
    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類(lèi),實(shí)現(xiàn)接口org.springframework.context.ApplicationListener中的onApplicationEvent方法,
 * 在該方法中只處理StudentAddEvent類(lèi)型的ApplicationEvent事件
 * 定義StudentAddListener監(jiān)聽(tīng)器
 */
[@Component](https://my.oschina.net/u/3907912)
public class StudentAddListener implements ApplicationListener {

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

        // 2.是增加學(xué)生事件的對(duì)象,進(jìn)行邏輯處理,比如記日志、積分等
        StudentAddEvent studentAddEvent = (StudentAddEvent) event;
        System.out.println("增加了學(xué)生:" + 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類(lèi),實(shí)現(xiàn)接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法,
 * 在構(gòu)造bean的時(shí)候注入Spring的上下文對(duì)象,以便通過(guò)Spring上下文對(duì)象的publishEvent方法來(lái)觸發(fā)StudentAddEvent事件
 */
@Component
public class StudentAddBean implements ApplicationContextAware {
    /**
     * 定義Spring上下文對(duì)象
     */
    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;

    }

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

}
package com.test.eventListener;

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

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

關(guān)于spring中怎么監(jiān)聽(tīng)ApplicationEvent事件現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI