亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot啟動后運(yùn)行代碼的幾種方法

 更新時(shí)間:2025年06月18日 09:23:49   作者:1010n111  
在開發(fā)SpringBoot應(yīng)用時(shí),有時(shí)需要在應(yīng)用啟動后執(zhí)行一些特定的代碼,例如監(jiān)控目錄變化、初始化數(shù)據(jù)等,然而,直接在啟動時(shí)運(yùn)行代碼可能會遇到@Autowired服務(wù)未初始化的問題,因此需要找到合適的時(shí)機(jī)來執(zhí)行這些代碼,所以本文給大家介紹了SpringBoot啟動后運(yùn)行代碼的幾種方法

Spring Boot啟動后運(yùn)行代碼的方法

實(shí)現(xiàn)步驟

1. 使用ApplicationReadyEvent

ApplicationReadyEvent會在應(yīng)用刷新并處理完所有相關(guān)回調(diào)后觸發(fā),表明應(yīng)用已準(zhǔn)備好處理請求??梢酝ㄟ^@EventListener注解監(jiān)聽該事件。

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

@Component
public class StartupRunner {

    @EventListener(ApplicationReadyEvent.class)
    public void doSomethingAfterStartup() {
        System.out.println("hello world, I have just started up");
    }
}

2. 實(shí)現(xiàn)ApplicationListener<ApplicationReadyEvent>接口

創(chuàng)建一個(gè)類實(shí)現(xiàn)ApplicationListener<ApplicationReadyEvent>接口,并重寫onApplicationEvent方法。

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(final ApplicationReadyEvent event) {
        // 這里編寫你的代碼
    }
}

3. 使用CommandLineRunner或ApplicationRunner

CommandLineRunnerApplicationRunner是Spring Boot提供的接口,實(shí)現(xiàn)這些接口并重寫run方法,Spring Boot會在應(yīng)用啟動過程的末尾執(zhí)行這些方法。

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Application started with command-line arguments: " + String.join(", ", args));
    }
}

4. 使用@PostConstruct注解

在一個(gè)@Component類中使用@PostConstruct注解的方法會在Bean初始化完成后執(zhí)行。

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;

@Component
public class Monitor {

    @PostConstruct
    public void init() {
        // 在這里啟動監(jiān)控
    }
}

5. 使用SmartInitializingSingleton

在Spring 4.1及以上版本中,可以使用SmartInitializingSingleton,它會在所有單例Bean初始化完成后回調(diào)。

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public SmartInitializingSingleton importProcessor() {
        return () -> {
            // 執(zhí)行任務(wù)
        };
    }
}

核心代碼

以下是幾種方法的核心代碼示例:

使用ApplicationReadyEvent

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

@Component
public class StartupExample {

    @EventListener(ApplicationReadyEvent.class)
    public void executeAfterStartup() {
        System.out.println("執(zhí)行啟動后任務(wù)");
    }
}

使用CommandLineRunner

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CommandLineExample implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("使用CommandLineRunner執(zhí)行任務(wù)");
    }
}

使用@PostConstruct

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;

@Component
public class PostConstructExample {

    @PostConstruct
    public void init() {
        System.out.println("使用@PostConstruct執(zhí)行任務(wù)");
    }
}

最佳實(shí)踐

  • 如果代碼需要在應(yīng)用準(zhǔn)備好處理請求后執(zhí)行,推薦使用ApplicationReadyEvent。
  • 如果需要處理命令行參數(shù),可以使用CommandLineRunner或ApplicationRunner。
  • 如果代碼是與Bean初始化相關(guān)的,可以使用@PostConstruct注解。
  • 如果需要在所有單例Bean初始化完成后執(zhí)行代碼,可以使用SmartInitializingSingleton。

常見問題

  • @Autowired服務(wù)未初始化:使用ApplicationReadyEvent、CommandLineRunner、ApplicationRunner或SmartInitializingSingleton可以確保@Autowired服務(wù)已初始化。
  • @PostConstruct方法執(zhí)行過早:@PostConstruct方法在Bean初始化時(shí)執(zhí)行,可能會在應(yīng)用整體準(zhǔn)備好之前執(zhí)行。如果需要在應(yīng)用準(zhǔn)備好后執(zhí)行,不建議使用該注解。
  • ContextRefreshedEvent多次觸發(fā):ContextRefreshedEvent可能會多次觸發(fā),需要在代碼中添加判斷邏輯,避免重復(fù)執(zhí)行。

以上就是SpringBoot啟動后運(yùn)行代碼的幾種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot啟動后運(yùn)行代碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Spring循環(huán)依賴的解決方案

    詳解Spring循環(huán)依賴的解決方案

    這篇文章主要介紹了詳解Spring循環(huán)依賴的解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • Java實(shí)現(xiàn)瀏覽器端大文件分片上傳

    Java實(shí)現(xiàn)瀏覽器端大文件分片上傳

    本文主要介紹了Java實(shí)現(xiàn)瀏覽器端大文件分片上傳,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Maven使用集成測試的示例代碼

    Maven使用集成測試的示例代碼

    本文介紹了在Maven項(xiàng)目中使用maven-failsafe-plugin插件進(jìn)行集成測試,步驟包括添加測試依賴、編寫集成測試類、配置插件、運(yùn)行測試以及查看和分析測試結(jié)果,感興趣的可以了解一下
    2024-11-11
  • IntelliJ IDEA查看方法說明文檔的圖解

    IntelliJ IDEA查看方法說明文檔的圖解

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA查看方法說明文檔的圖解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Java和Scala集合間的相互轉(zhuǎn)換方式

    Java和Scala集合間的相互轉(zhuǎn)換方式

    這篇文章主要介紹了Java和Scala集合間的相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java如何定義Long類型

    Java如何定義Long類型

    這篇文章主要介紹了Java如何定義Long類型,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼

    mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼

    這篇文章主要介紹了mybatis中insert主鍵ID獲取和多參數(shù)傳遞的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • jar包中替換指定的class文件方法詳解

    jar包中替換指定的class文件方法詳解

    這篇文章主要為大家介紹了jar包中替換指定的class文件方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 解決IDEA 左側(cè)Project中沒有out文件夾的問題

    解決IDEA 左側(cè)Project中沒有out文件夾的問題

    這篇文章主要介紹了解決IDEA 左側(cè)Project中沒有out文件夾的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • SpringBoot集成Redis,并自定義對象序列化操作

    SpringBoot集成Redis,并自定義對象序列化操作

    這篇文章主要介紹了SpringBoot集成Redis,并自定義對象序列化操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評論