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

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

 更新時間:2025年06月18日 09:23:49   作者:1010n111  
在開發(fā)SpringBoot應(yīng)用時,有時需要在應(yīng)用啟動后執(zhí)行一些特定的代碼,例如監(jiān)控目錄變化、初始化數(shù)據(jù)等,然而,直接在啟動時運(yùn)行代碼可能會遇到@Autowired服務(wù)未初始化的問題,因此需要找到合適的時機(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)建一個類實(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注解

在一個@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初始化時執(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)文章

  • java中Class類的基礎(chǔ)知識點(diǎn)及實(shí)例

    java中Class類的基礎(chǔ)知識點(diǎn)及實(shí)例

    在本篇文章里小編給大家分享了關(guān)于java中Class類的基礎(chǔ)知識點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • springboot Validated失效的問題及解決思路

    springboot Validated失效的問題及解決思路

    文章主要介紹了Java Bean Validation(JSR 303/JSR 349)和Hibernate Validator的基本用法,包括常用注解的使用、@Valid和@Validated注解的區(qū)別、如何自定義校驗(yàn)注解以及如何在Spring Boot中使用這些校驗(yàn)機(jī)制
    2026-01-01
  • sprinboot項(xiàng)目啟動一半到圖形化界面卡住了的解決

    sprinboot項(xiàng)目啟動一半到圖形化界面卡住了的解決

    這篇文章主要介紹了sprinboot項(xiàng)目啟動一半到圖形化界面卡住了的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java SpringBoot安全框架整合Spring Security詳解

    Java SpringBoot安全框架整合Spring Security詳解

    這篇文章主要介紹了Spring Boot整合Spring Security的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • spring事務(wù)的REQUIRES_NEW源碼示例解析

    spring事務(wù)的REQUIRES_NEW源碼示例解析

    這篇文章主要為大家介紹了spring事務(wù)的REQUIRES_NEW源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動態(tài)路由

    Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動態(tài)路由

    這篇文章主要介紹了Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動態(tài)路由的方法,幫助大家實(shí)現(xiàn)路由信息的自動更新,感興趣的朋友可以了解下
    2020-10-10
  • Springboot3+Vue3實(shí)現(xiàn)JWT登錄鑒權(quán)功能

    Springboot3+Vue3實(shí)現(xiàn)JWT登錄鑒權(quán)功能

    JWT用于在網(wǎng)絡(luò)應(yīng)用間安全的傳遞消息,它以緊湊且自包含的方式,通過JSON對象在各方之間傳遞經(jīng)過驗(yàn)證的信息,這篇文章主要介紹了Springboot3+Vue3實(shí)現(xiàn)JWT登錄鑒權(quán)功能,需要的朋友可以參考下
    2025-03-03
  • SpringBoot中的@Value注解用法

    SpringBoot中的@Value注解用法

    這篇文章主要介紹了SpringBoot中的@Value注解用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 使用Spring?Security搭建極簡的安全網(wǎng)站教程

    使用Spring?Security搭建極簡的安全網(wǎng)站教程

    這篇文章主要為大家介紹了使用Spring?Security搭建極簡的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Java Stream 的 limit 與 skip 使用場景操作分析

    Java Stream 的 limit 與 skip 使用場

    Java Stream的limit與skip操作用于控制元素數(shù)量,limit截取前N個元素,具備短路特性,適合無限流和性能優(yōu)化,skip跳過前N個元素,常用于分頁,兩者結(jié)合可實(shí)現(xiàn)數(shù)據(jù)切片,但需注意順序依賴性和性能陷阱,本文介紹Java Stream的limit與skip使用場景操作分析,感興趣的朋友一起看看吧
    2025-07-07

最新評論

瓮安县| 柘城县| 凤凰县| 玉山县| 五台县| 田林县| 南通市| 东明县| 浪卡子县| 固原市| 金门县| 贺州市| 长丰县| 丘北县| 巴里| 武威市| 潜山县| 沁水县| 峨边| 临江市| 佛教| 合作市| 晴隆县| 德兴市| 延边| 黔西县| 宁国市| 西平县| 原平市| 韩城市| 长丰县| 会昌县| 德清县| 元阳县| 千阳县| 固镇县| 民乐县| 来凤县| 兰坪| 康马县| 兴化市|