Springboot事件監(jiān)聽

先看一個demo,加入依賴

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
</parent>

<dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
     </dependency>
</dependencies>

定義一個自定義事件,繼承ApplicationEvent類

/**
 * 定義事件
 *
 */
public class MyApplicationEvent extends ApplicationEvent {

    private static final long serialVersionUID = 1L;

    public MyApplicationEvent(Object source) {
        super(source);
    }
}

定義一個事件監(jiān)聽器MyApplicationListener實現(xiàn)ApplicationListener接口,

package com.zhihao.miao;

import org.springframework.context.ApplicationListener;

public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接收到事件:"+event.getClass());
    }

}

主測試類:

package com.zhihao.miao;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        //配置事件監(jiān)聽器
        application.addListeners(new MyApplicationListener());
        ConfigurableApplicationContext context =application.run(args);
        //發(fā)布事件
        context.publishEvent(new MyApplicationEvent(new Object()));
        context.close();
    }
}

打印結(jié)果:


總結(jié):
springboot事件監(jiān)聽的流程:

  1. 自定義事件,一般是繼承ApplicationEvent抽象類。
  2. 定義事件監(jiān)聽器,一般是實現(xiàn)ApplicationListener接口。
  3. 配置監(jiān)聽器,啟動的時候,需要把監(jiān)聽器加入到spring容器中。
  4. 發(fā)布事件。

其中第三步(將監(jiān)聽器納入到spring容器)除了上面的方法之外,

application.addListeners(new MyApplicationListener());

還有三種方法

第二種方式
直接在MyApplicationListener類上加上@Component注解,納入spring容器管理

package com.zhihao.miao;

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

@Component
public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {

    public void onApplicationEvent(MyApplicationEvent event) {
        System.out.println("接收到事件:"+event.getClass());
    }

}

主類測試:

package com.zhihao.miao;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        ConfigurableApplicationContext context =application.run(args);
        //發(fā)布事件
        context.publishEvent(new MyApplicationEvent(new Object()));
        context.close();
    }
}

第三種方式
在配置文件中配置

context.listener.classes=com.zhihao.miao.MyApplicationListener

源碼分析:
進入DelegatingApplicationListener類中的onApplicationEvent方法,getListeners是獲取當前項目中的所有事件監(jiān)聽器。

源碼分析

第四種方式
使用@EventListener注解

package com.zhihao.miao;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyEventHandle {


    /**
     * 參數(shù)任意(為Object)的時候所有事件都會監(jiān)聽到
     * 所有,該參數(shù)事件,或者其子事件(子類)都可以接收到
     */
    @EventListener
    public void event(Object event){
        System.out.println("MyEventHandle 接收到事件:" + event.getClass());
    }
    
}

主類測試:

package com.zhihao.miao;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        ConfigurableApplicationContext context =application.run(args);
        //發(fā)布事件
        context.publishEvent(new MyApplicationEvent(new Object()));
        context.close();
    }
}

打印結(jié)果:

源碼分析:
進入@EventListener注解,文檔說明中處理@EventListener是依靠EventListenerMethodProcessorbean,然后進入EventListenerMethodProcessorbean中,我們大概看一下流程,可以自己調(diào)試

圖片.png

總結(jié)
配置事件監(jiān)聽器的四種方法

  1. SpringApplication.addListeners 添加監(jiān)聽器
  2. 把監(jiān)聽器納入到spring容器中管理
  3. 使用context.listener.classes配置項配置(詳細內(nèi)容參照:DelegatingApplicationListener)
  4. 使用@EventListener注解,在方法上面加入@EventListener注解,且該類需要納入到spring容器中管理(詳細內(nèi)容參照:EventListenerMethodProcessor,EventListenerFactory)

spring及springboot已經(jīng)定義好的事件

spring的事件


springboot的事件


測試一下spring自帶的事件:

@Component
public class MyEventHandle {

    /**
     * 監(jiān)聽spring的事件(運用停止事件,Application.stop()方法時候監(jiān)聽到。
     *
     */
    @EventListener
    public void eventStop(ContextStoppedEvent event){
        System.out.println("應(yīng)用停止事件==========:"+event.getClass());
    }
}

主類測試:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class,args);
        context.stop();
    }
}

測試:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,254評論 6 342
  • spring官方文檔:http://docs.spring.io/spring/docs/current/spri...
    牛馬風情閱讀 1,842評論 0 3
  • 我的愛 我聞到 初春河邊柳樹間肆意擺動的嫩綠 像要招引鳥兒的棲息 我聽到 盛夏金波碧浪的原野上曲調(diào)婉轉(zhuǎn)的夜鶯 像要...
    王團長1閱讀 417評論 0 6
  • 說服客戶:怎么說服客戶采用你的產(chǎn)品,相信我我就講, 不相信我就不講了。 說話水準:說話要有水準,說替我公司...
    D051飛鷹閱讀 203評論 0 2

友情鏈接更多精彩內(nèi)容