最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中ApplicationListener的使用解析

 更新時間:2023年12月25日 10:59:57   作者:云川之下  
這篇文章主要介紹了Spring中ApplicationListener的使用解析,ApplicationContext事件機(jī)制是觀察者設(shè)計(jì)模式的實(shí)現(xiàn),通過ApplicationEvent類和ApplicationListener接口,需要的朋友可以參考下

背景

ApplicationContext事件機(jī)制是觀察者設(shè)計(jì)模式的實(shí)現(xiàn),通過ApplicationEvent類和ApplicationListener接口,可以實(shí)現(xiàn)ApplicationContext事件處理;

如果容器中存在ApplicationListener的Bean,當(dāng)ApplicationContext調(diào)用publishEvent方法時,對應(yīng)的Bean會被觸發(fā)。

spring內(nèi)置事件

  • ContextRefreshedEvent ApplicationContext 被初始化或刷新時,該事件被觸發(fā)。這也可以在 ConfigurableApplicationContext接口中使用 refresh() 方法來發(fā)生。此處的初始化是指:所有的Bean被成功裝載,后處理Bean被檢測并激活,所有Singleton Bean 被預(yù)實(shí)例化,ApplicationContext容器已就緒可用
  • ContextStartedEvent 當(dāng)使用 ConfigurableApplicationContext (ApplicationContext子接口)接口中的 start() 方法啟動 ApplicationContext 時,該事件被發(fā)布。你可以調(diào)查你的數(shù)據(jù)庫,或者你可以在接受到這個事件后重啟任何停止的應(yīng)用程序。
  • ContextStoppedEvent 當(dāng)使用 ConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 時,發(fā)布這個事件。你可以在接受到這個事件后做必要的清理的工作。
  • ContextClosedEvent 當(dāng)使用 ConfigurableApplicationContext 接口中的 close() 方法關(guān)閉 ApplicationContext 時,該事件被發(fā)布。一個已關(guān)閉的上下文到達(dá)生命周期末端;它不能被刷新或重啟。
  • RequestHandledEvent 這是一個 web-specific 事件,告訴所有 bean HTTP 請求已經(jīng)被服務(wù)。只能應(yīng)用于使用DispatcherServlet的Web應(yīng)用。在使用Spring作為前端的MVC控制器時,當(dāng)Spring處理用戶請求結(jié)束后,系統(tǒng)會自動觸發(fā)該事件。

同樣事件可以自定義、監(jiān)聽也可以自定義,完全根據(jù)自己的業(yè)務(wù)邏輯來處理。

ApplicationListener源碼

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    /**
     * Handle an application event.
     * @param event the event to respond to
     */
    void onApplicationEvent(E event);
}

ContextRefreshedEvent事件的監(jiān)聽

以Spring的內(nèi)置事件ContextRefreshedEvent為例,當(dāng)ApplicationContext被初始化或刷新時,會觸發(fā)ContextRefreshedEvent事件,下面我們就實(shí)現(xiàn)一個ApplicationListener來監(jiān)聽此事件的發(fā)生。

@Component
public class MyListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("容器中初始化Bean數(shù)量:" + event.getApplicationContext().getBeanDefinitionCount());
    }
}

啟動服務(wù),可以看到

在這里插入圖片描述

至此,便完成了一個事件及監(jiān)聽類的實(shí)現(xiàn)和實(shí)例化。

自定義事件及監(jiān)聽,以發(fā)送郵件為例

自定義郵件通知事件類:EmailEvent

package com.lw.coodytest.event;
import org.springframework.context.ApplicationEvent;
/**
 * @Classname EmailEvent
 * @Description 郵件通知事件
 * @Author lw
 * @Date 2019-12-20 11:05
 */
public class EmailEvent extends ApplicationEvent {
    private String email;
    private String content;
    public EmailEvent(Object source){
        super(source);
    }
    public EmailEvent(Object source, String email, String content){
        super(source);
        this.email = email;
        this.content = content;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

自定義郵件通知監(jiān)聽類:EmailListener:

package com.lw.coodytest.event;
import org.springframework.context.ApplicationEvent;
/**
 * @Classname EmailEvent
 * @Description 郵件通知事件
 * @Author lw
 * @Date 2019-12-20 11:05
 */
public class EmailEvent extends ApplicationEvent {
    private String email;
    private String content;
    public EmailEvent(Object source){
        super(source);
    }
    public EmailEvent(Object source, String email, String content){
        super(source);
        this.email = email;
        this.content = content;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

注意:onApplicationEvent方法的入?yún)ⅲ仨毷亲远x的那個類型

單元測試類:

package com.lw.coodytest.junit;
import com.lw.coodytest.event.EmailEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;
/**
 * @Classname ListenerTest
 * @Description 監(jiān)聽測試類
 * @Author lw
 * @Date 2019-12-20 11:12
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ListenerTest {
    @Autowired
    private WebApplicationContext webapplicationcontext;
    @Test
    public void testListener(){
        EmailEvent emailEvent = new EmailEvent("object", "172572575@qq.com", "###listener");
        webapplicationcontext.publishEvent(emailEvent);
    }
}

監(jiān)聽器通過@Component注解進(jìn)行實(shí)例化,并在onApplicationEvent中打印相關(guān)信息: 執(zhí)行測試類,可以看到

在這里插入圖片描述

至此,便完成了一個自定義事件及監(jiān)聽類的實(shí)現(xiàn)和實(shí)例化。

特別注意:不管是內(nèi)置監(jiān)聽還是外部自定義監(jiān)聽一定要把實(shí)現(xiàn)ApplicationListener的類定義成一個bean才行,可以通過注解@Component或者在bean.xml中定義來實(shí)現(xiàn)。

也就是說通過注冊為bean,才能實(shí)現(xiàn)事件的綁定。

到此這篇關(guān)于Spring中ApplicationListener的使用解析的文章就介紹到這了,更多相關(guān)ApplicationListener的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Security密碼編輯器示例詳解

    Spring Security密碼編輯器示例詳解

    本文介紹了Spring Security的密碼編碼機(jī)制及其在Maven項(xiàng)目管理中的配置方式,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-10-10
  • 使用SpringBoot編寫一個優(yōu)雅的單元測試

    使用SpringBoot編寫一個優(yōu)雅的單元測試

    這篇文章主要為大家詳細(xì)介紹了如何使用SpringBoot編寫一個優(yōu)雅的單元測試,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-07-07
  • Spring5中SpringWebContext方法過時的解決方案

    Spring5中SpringWebContext方法過時的解決方案

    這篇文章主要介紹了Spring5中SpringWebContext方法過時的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 關(guān)于SpringBoot靜態(tài)資源路徑管理問題

    關(guān)于SpringBoot靜態(tài)資源路徑管理問題

    這篇文章主要介紹了SpringBoot靜態(tài)資源路徑管理,主要包括默認(rèn)靜態(tài)資源路徑,增加靜態(tài)資源路徑前綴的相關(guān)操作,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數(shù)據(jù)庫密碼加密的方法

    SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數(shù)據(jù)庫

    這篇文章主要介紹了SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數(shù)據(jù)庫密碼加密的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot集成POI導(dǎo)出Execl表格之統(tǒng)一工具類

    SpringBoot集成POI導(dǎo)出Execl表格之統(tǒng)一工具類

    這篇文章主要為大家詳細(xì)介紹了SpringBoot集成POI導(dǎo)出Execl表格之統(tǒng)一工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 編寫Spring MVC控制器的14個技巧(小結(jié))

    編寫Spring MVC控制器的14個技巧(小結(jié))

    這篇文章主要介紹了編寫Spring MVC控制器的14個技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • SpringBoot因?yàn)閖ackson版本問題啟動失敗的解決

    SpringBoot因?yàn)閖ackson版本問題啟動失敗的解決

    SpringBoot啟動失敗解決Jackson版本沖突,通過調(diào)整pom.xml中的Jackson依賴版本,確保與SpringBoot兼容,避免啟動時的嵌入容器初始化異常
    2026-05-05
  • Java中實(shí)現(xiàn)對中文字符的精確判斷的方法

    Java中實(shí)現(xiàn)對中文字符的精確判斷的方法

    在開發(fā)涉及多語言處理的應(yīng)用時,經(jīng)常需要對輸入的文本進(jìn)行語言或字符類型的判斷,特別是在中文環(huán)境下,準(zhǔn)確地識別中文字符對于文本處理、數(shù)據(jù)驗(yàn)證等操作至關(guān)重要,本文將介紹如何在Java中實(shí)現(xiàn)對中文字符的精確判斷,需要的朋友可以參考下
    2025-12-12
  • java中redis增刪查以及清理緩存的案例

    java中redis增刪查以及清理緩存的案例

    這篇文章主要介紹了java中redis增刪查以及清理緩存的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02

最新評論

汉沽区| 富平县| 遂溪县| 广西| 兴业县| 海林市| 伽师县| 邢台县| 图们市| 盐山县| 井研县| 乌兰县| 镇赉县| 萝北县| 张家界市| 盐城市| 衡山县| 咸阳市| 山西省| 嘉祥县| 临泉县| 万安县| 肥西县| 敖汉旗| 上栗县| 永康市| 兴安县| 民乐县| 德惠市| 潮州市| 楚雄市| 宁都县| 长乐市| 凤庆县| 库尔勒市| 大名县| 佛山市| 庆城县| 大宁县| 高密市| 青铜峡市|