Spring自定義監聽事件

2021-10-11 13:27:00 字數 1040 閱讀 3543

可以監聽物件的建立資料。

自定義監聽事件可以監聽容器變化,同時也能精確定位指定事件物件,我們編寫乙個案例演示自定義監聽事件實現流程。

定義事件監聽物件: messagenotifier

import org.springframework.context.event.contextrefreshedevent;

/** * 自定義監聽事件

*/ //監聽觸發執行

@override

system.out.println("接受到事件傳遞的資料:"+source);

system.out.println("監聽到了對應事件!");

}} 定義事件物件: messageevent

/** * 定義事件物件

*/ private string phone;

private string message;

public messageevent(object source,string phone,string message)

public messageevent(object source)

public string getphone()

public void setphone(string phone)

public string getmessage()

public void setmessage(string message) }

將物件新增到容器中:

新增事件測試:

public class springcontexttest 

}

列印結果

事件物件:org.springframework.context.event.contextrefreshedevent

監聽到了對應事件!

接受到事件傳遞的資料:beijing

事件物件:com.h.spring.messageevent

監聽到了對應事件!

spring的自定義監聽事件

今天寫 的時候遇到了 然後我就鬼使神差的翻了一下spring框架文件,看到了自定義監聽事件,下面是我的收穫分享。spring中自帶有5種標準事件,contextrefreshedevent,contextstartedevent,contextstoppedevent,contextclosedev...

Spring 監聽事件

contextclosedevent 上下文關閉的時候發布的事件 contextrefreshedevent 上下文初始化或者重新整理的時候發布的事件 requesthandledevent 上下文中,當乙個請求被處理後發布的事件 b 自定義事件及事件的監聽 b 2 發布事件 context.pub...

Spring自定義事件

如果僅僅使用 spring 的內定事件,那顯然是遠遠不夠的,幸好,spring 為我們提供了中自定義發布事件的能力。下面通過例程 來展示如何發布並監聽自定義的事件。在工程中,我們定義乙個 animal 類,為受管 bean 它具有乙個 speak 方法,我們要做的就是監視該方法,當使用者呼叫該方法時...