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

Spring中的SpringApplicationRunListener詳細(xì)解析

 更新時間:2023年11月15日 08:38:22   作者:阿孟呀  
這篇文章主要介紹了Spring中的SpringApplicationRunListener詳細(xì)解析,SpringApplicationRunListener是一個監(jiān)聽SpringApplication中run方法的接口,在項目啟動過程的各個階段進(jìn)行事件的發(fā)布,需要的朋友可以參考下

前言

SpringApplicationRunListener是一個監(jiān)聽SpringApplication中run方法的接口。在項目啟動過程的各個階段進(jìn)行事件的發(fā)布。

實現(xiàn)SpringApplicationRunListener接口的類由類路徑下加載(META-INF/spring.factories)進(jìn)行初始化。

想要實現(xiàn)SpringApplicationRunListener接口,需要提供能夠接收SpringApplication和String[] args的構(gòu)造器。

SpringApplication.run(CommonsTestApplication.class, args);
                                                            

SpringApplicationRunListener 類方法

SpringApplicationRunListener 的方法在項目啟動的各個階段提供事件發(fā)布的接口。因此可以通過實現(xiàn)SpringApplicationRunListener 接口,達(dá)到在啟動的各個過程中進(jìn)行擴(kuò)展

public interface SpringApplicationRunListener {
 
	
	void starting();//run方法開始執(zhí)行,發(fā)布ApplicationStartingEvent事件
 
	//環(huán)境準(zhǔn)備好時,發(fā)布ApplicationEnvironmentPreparedEvent事件
	void environmentPrepared(ConfigurableEnvironment environment);
 
    //容器的上下文準(zhǔn)備初始化完畢,發(fā)布ApplicationContextInitializedEvent
	void contextPrepared(ConfigurableApplicationContext context);
 
	//上下文加載配置時候,對應(yīng)ApplicationPreparedEvent
	void contextLoaded(ConfigurableApplicationContext context);
 
	//上下文刷新且應(yīng)用啟動時,并且在CommandLineRunner和ApplicationRunners還沒喚醒前,發(fā)布程序以及啟動事件ApplicationStartedEvent
	void started(ConfigurableApplicationContext context);
 
	//在上下文刷新,應(yīng)用已經(jīng)啟動,在CommandLineRunner和ApplicationRunners喚醒后,并且在run方法快執(zhí)行結(jié)束前執(zhí)行,發(fā)布ApplicationReadyEvent,代表程序已經(jīng)準(zhǔn)備好
	void running(ConfigurableApplicationContext context);
 
	//當(dāng)運行程序失敗時,發(fā)布ApplicationFailedEvent事件。
	void failed(ConfigurableApplicationContext context, Throwable exception);
 
}

實現(xiàn)

1.實現(xiàn)SpringApplicationRunListener接口

注意添加能夠接收SpringApplication 和String[] 的構(gòu)造器

在started方法中添加了自定義的事件發(fā)布。

package com.wzx.listener;
 
import com.wzx.event.TestApplication;
import com.wzx.event.TestApplicationEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
 
/**
 * @Description: TODO
 * @author: wengzx
 * @date: 2021年04月14日 16:01
 */
public class TestSpringApplicationRunListener implements SpringApplicationRunListener {
 
    private final SpringApplication application;
 
    private final String[] args;
 
 
 
    public TestSpringApplicationRunListener(SpringApplication application, String[] args1) {
        this.application = application;
        this.args = args1;
    }
 
    @Override
    public void starting() {
        System.out.println("TestSpringApplicationRunListener is starting");
    }
 
    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        System.out.println("TestSpringApplicationRunListener is environmentPrepared");
    }
 
    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        System.out.println("TestSpringApplicationRunListener is contextPrepared");
 
    }
 
    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
 
        System.out.println("TestSpringApplicationRunListener is contextLoaded");
    }
 
    @Override
    public void started(ConfigurableApplicationContext context) {
        System.out.println("TestSpringApplicationRunListener is started");
        TestApplication testApplication = new TestApplication();
        testApplication.setName("zhangsan");
        testApplication.setAge(23);
        TestApplicationEvent testApplicationEvent = new TestApplicationEvent(testApplication);
        context.publishEvent(testApplicationEvent);
    }
 
    @Override
    public void running(ConfigurableApplicationContext context) {
        System.out.println("TestSpringApplicationRunListener is running");
    }
 
    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        System.out.println("TestSpringApplicationRunListener is failed");
    }
}

2.配置META-INF/spring.factories

3.在SpringApplication.run(String... args)方法內(nèi)斷點啟動程序,可以發(fā)現(xiàn)獲得了自定義的程序監(jiān)聽器。

4.查看控制臺輸出

可以看出在各個階段的輸出,可以此類來擴(kuò)展程序。

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

相關(guān)文章

  • Java中實現(xiàn)樹形菜單的兩種方式

    Java中實現(xiàn)樹形菜單的兩種方式

    這篇文中,我一共會用兩種方式來實現(xiàn)目錄樹的數(shù)據(jù)結(jié)構(gòu),兩種寫法邏輯是一樣的,只是一種適合新手理解,一種看著簡單明了但是對于小白不是很好理解,在這里我會很詳細(xì)的講解每一步代碼,主要是方便新人看懂,彌補(bǔ)曾經(jīng)自己學(xué)習(xí)過程中的苦惱,需要的朋友可以參考下
    2023-09-09
  • Java中@RequiredArgsConstructor使用詳解

    Java中@RequiredArgsConstructor使用詳解

    這篇文章主要介紹了Java中@RequiredArgsConstructor使用的相關(guān)資料,@RequiredArgsConstructor是Lombok庫提供的一個注解,用于自動生成一個包含所有final字段和非空字段的構(gòu)造函數(shù),需要的朋友可以參考下
    2025-05-05
  • 淺談java獲取服務(wù)器基本信息

    淺談java獲取服務(wù)器基本信息

    這篇文章主要介紹了java獲取服務(wù)器基本信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • JavaMail實現(xiàn)發(fā)送郵件功能

    JavaMail實現(xiàn)發(fā)送郵件功能

    這篇文章主要為大家詳細(xì)介紹了JavaMail實現(xiàn)發(fā)送郵件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Spring中讀取配置文件的五種方式

    Spring中讀取配置文件的五種方式

    在使用spring或springboot項目開發(fā)中,難免會涉及到讀取配置文件的各種配置參數(shù)的情況,因為當(dāng)項目的規(guī)模上去之后,在單個配置文件中維護(hù)所有的配置信息很難滿足實際的需要,所以本文給大家介紹了Spring讀取配置文件多種方式,需要的朋友可以參考下
    2024-04-04
  • java連接SQL?Server數(shù)據(jù)庫圖文教程(自用)

    java連接SQL?Server數(shù)據(jù)庫圖文教程(自用)

    在Java應(yīng)用程序中,我們經(jīng)常需要與數(shù)據(jù)庫進(jìn)行交互,下面這篇文章主要給大家介紹了關(guān)于java連接SQL?Server數(shù)據(jù)庫的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • 滴滴二面之Kafka如何讀寫副本消息的

    滴滴二面之Kafka如何讀寫副本消息的

    這篇文章主要給大家介紹了關(guān)于滴滴二面之Kafka如何讀寫副本消息的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-01-01
  • Java代碼實現(xiàn)循環(huán)隊列的示例代碼

    Java代碼實現(xiàn)循環(huán)隊列的示例代碼

    隊列作為基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu),是程序員的入門課。也是所有程序員必須掌握的一種數(shù)據(jù)結(jié)構(gòu),隊列在程序中廣泛應(yīng)用,因此我們應(yīng)該對隊列有深入的了解,接下來我們通過代碼來對隊列這種數(shù)據(jù)結(jié)構(gòu)進(jìn)行深度解析,感興趣的朋友一起看看吧
    2021-09-09
  • Java中的System.arraycopy()淺復(fù)制方法詳解

    Java中的System.arraycopy()淺復(fù)制方法詳解

    這篇文章主要介紹了Java中的System.arraycopy()淺復(fù)制方法詳解,Java數(shù)組的復(fù)制操作可以分為深度復(fù)制和淺度復(fù)制,簡單來說深度復(fù)制,可以將對象的值和對象的內(nèi)容復(fù)制;淺復(fù)制是指對對象引用的復(fù)制,需要的朋友可以參考下
    2023-11-11
  • java 工廠模式的講解及優(yōu)缺點的介紹

    java 工廠模式的講解及優(yōu)缺點的介紹

    這篇文章主要介紹了java 工廠模式的講解及優(yōu)缺點的介紹的相關(guān)資料, 簡單工廠模式,又稱為靜態(tài)工廠方法(Static Factory Method)模式,它屬于類創(chuàng)建型模式,需要的朋友可以參考下
    2017-08-08

最新評論

湘潭县| 图们市| 扶风县| 赤峰市| 平昌县| 清镇市| 临湘市| 西峡县| 仁寿县| 冷水江市| 东阿县| 五寨县| 晴隆县| 富裕县| 龙南县| 西和县| 三亚市| 东阿县| 永胜县| 湘西| 石楼县| 沙洋县| 宜章县| 垫江县| 湛江市| 海林市| 清镇市| 凉城县| 漠河县| 琼海市| 红河县| 堆龙德庆县| 方正县| 禹城市| 格尔木市| 华阴市| 浠水县| 潞城市| 迁西县| 兴海县| 沂水县|