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

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

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

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

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

1. 使用ApplicationReadyEvent

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

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è)類(lèi)實(shí)現(xiàn)ApplicationListener<ApplicationReadyEvent>接口,并重寫(xiě)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) {
        // 這里編寫(xiě)你的代碼
    }
}

3. 使用CommandLineRunner或ApplicationRunner

CommandLineRunnerApplicationRunner是Spring Boot提供的接口,實(shí)現(xiàn)這些接口并重寫(xiě)run方法,Spring Boot會(huì)在應(yīng)用啟動(dòng)過(guò)程的末尾執(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類(lèi)中使用@PostConstruct注解的方法會(huì)在Bean初始化完成后執(zhí)行。

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

@Component
public class Monitor {

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

5. 使用SmartInitializingSingleton

在Spring 4.1及以上版本中,可以使用SmartInitializingSingleton,它會(huì)在所有單例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í)行啟動(dòng)后任務(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)備好處理請(qǐng)求后執(zhí)行,推薦使用ApplicationReadyEvent。
  • 如果需要處理命令行參數(shù),可以使用CommandLineRunner或ApplicationRunner。
  • 如果代碼是與Bean初始化相關(guān)的,可以使用@PostConstruct注解。
  • 如果需要在所有單例Bean初始化完成后執(zhí)行代碼,可以使用SmartInitializingSingleton。

常見(jiàn)問(wèn)題

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

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

相關(guān)文章

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

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

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

    springboot Validated失效的問(wèn)題及解決思路

    文章主要介紹了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)目啟動(dòng)一半到圖形化界面卡住了的解決

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

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

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

    這篇文章主要介紹了Spring Boot整合Spring Security的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    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)動(dòng)態(tài)路由

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

    這篇文章主要介紹了Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由的方法,幫助大家實(shí)現(xiàn)路由信息的自動(dòng)更新,感興趣的朋友可以了解下
    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)用間安全的傳遞消息,它以緊湊且自包含的方式,通過(guò)JSON對(duì)象在各方之間傳遞經(jīng)過(guò)驗(yàn)證的信息,這篇文章主要介紹了Springboot3+Vue3實(shí)現(xiàn)JWT登錄鑒權(quán)功能,需要的朋友可以參考下
    2025-03-03
  • SpringBoot中的@Value注解用法

    SpringBoot中的@Value注解用法

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

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

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

    Java Stream 的 limit 與 skip 使用場(chǎng)

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

最新評(píng)論

清丰县| 台东市| 肃北| 宁陵县| 呈贡县| 焦作市| 祁东县| 两当县| 旌德县| 淅川县| 通河县| 寿宁县| 东莞市| 临湘市| 忻州市| 滦南县| 崇州市| 华容县| 天津市| 中江县| 依安县| 威远县| 宜章县| 怀远县| 公主岭市| 辽阳市| 探索| 修文县| 济南市| 海伦市| 府谷县| 镇远县| 淳安县| 呈贡县| 商洛市| 塘沽区| 廊坊市| 双鸭山市| 阳东县| 巴塘县| 仪征市|