欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot啟動(dòng)后自動(dòng)執(zhí)行方法的各種方式對比

 更新時(shí)間:2025年04月29日 10:16:13   作者:饕餮爭鋒  
這篇文章主要為大家詳細(xì)介紹了SpringBoot啟動(dòng)后自動(dòng)執(zhí)行方法的各種方式和性能對比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下

1. SpringBoot啟動(dòng)后自動(dòng)執(zhí)行方法的各種方式

1.1 @PostConstruct 注解

作用:在依賴注入完成后執(zhí)行初始化方法。

適用場景:需要在Bean初始化時(shí)執(zhí)行某些操作(如配置、預(yù)加載數(shù)據(jù))。

注意:該方法在Bean初始化階段執(zhí)行,此時(shí)應(yīng)用可能未完全啟動(dòng)(如數(shù)據(jù)庫連接未就緒)。

示例代碼:

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
 
@Component
public class MyPostConstructBean {
    @PostConstruct
    public void init() {
        System.out.println("PostConstruct 方法執(zhí)行");
        // 執(zhí)行初始化邏輯
    }
}

1.2. 實(shí)現(xiàn) InitializingBean 接口

作用:通過重寫 afterPropertiesSet 方法實(shí)現(xiàn)初始化。

適用場景:需要在屬性注入后執(zhí)行操作,與 @PostConstruct 類似但需實(shí)現(xiàn)接口。

示例代碼:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
 
@Component
public class MyInitializingBean implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean 的 afterPropertiesSet 方法執(zhí)行");
        // 執(zhí)行初始化邏輯
    }
}

1.3. 實(shí)現(xiàn) ApplicationRunner 接口

作用:在Spring應(yīng)用上下文刷新后執(zhí)行,支持接收命令行參數(shù)。

適用場景:需要在應(yīng)用啟動(dòng)完成后執(zhí)行操作,此時(shí)Bean已初始化完畢。

示例代碼:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner 執(zhí)行");
        // 執(zhí)行邏輯,可訪問命令行參數(shù) args
    }
}

1.4. 實(shí)現(xiàn) CommandLineRunner 接口

作用:與 ApplicationRunner 類似,但參數(shù)為 String[]。

適用場景:需要接收簡單命令行參數(shù)時(shí)使用。

示例代碼:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner 執(zhí)行");
        // 執(zhí)行邏輯,可訪問 args 參數(shù)
    }
}

1.5. 實(shí)現(xiàn) ApplicationListener 接口

作用:通過 ApplicationListener 監(jiān)聽?wèi)?yīng)用完全啟動(dòng)事件。

適用場景:需要在應(yīng)用完全就緒后執(zhí)行操作(如啟動(dòng)后發(fā)送通知)。

示例代碼:

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("ApplicationReadyEvent 觸發(fā),應(yīng)用已啟動(dòng)");
        // 執(zhí)行邏輯,此時(shí)可安全訪問所有Bean
    }
}

1.6. 使用 @Bean 的 initMethod 屬性

作用:在Spring配置類中定義Bean時(shí),通過 initMethod 指定初始化方法。

示例代碼:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class AppConfig {
 
    @Bean(initMethod = "init")
    public MyBean myBean() {
        return new MyBean();
    }
}
 
public class MyBean {
    public void init() {
        System.out.println("應(yīng)用啟動(dòng)后執(zhí)行: initMethod");
        // 在這里添加初始化邏輯
    }
}

或使用 init-method 屬性(XML 配置):

<bean id="myBean" class="com.example.MyBean" init-method="init"/>

2.各方式執(zhí)行時(shí)機(jī)對比

方法執(zhí)行時(shí)機(jī)
@PostConstructBean 初始化完成后(屬性注入后)
InitializingBean與 @PostConstruct 類似,屬性注入后
ApplicationRunner應(yīng)用上下文刷新后,ApplicationReadyEvent 觸發(fā)前
CommandLineRunner同上
ApplicationListener應(yīng)用完全就緒(所有Bean初始化完成,所有啟動(dòng)任務(wù)執(zhí)行完畢)

根據(jù)需求選擇合適的方式:

  • 需要訪問數(shù)據(jù)庫或資源時(shí),建議使用 ApplicationListener 或 CommandLineRunner。
  • 簡單的Bean初始化邏輯可用 @PostConstruct 或 InitializingBean。

注意事項(xiàng)

  • 線程問題:默認(rèn)在主線程執(zhí)行,避免長時(shí)間阻塞操作,可考慮異步執(zhí)行(如結(jié)合 @Async)。
  • 依賴注入:在 @PostConstruct 和 InitializingBean 中無法直接訪問其他Bean(需等待初始化完成)。
  • 順序控制:通過 @Order 或?qū)崿F(xiàn) Ordered 接口控制多個(gè)啟動(dòng)任務(wù)的順序。

相關(guān)文檔:Spring中InitializingBean接口和@PostConstruct注解的使用詳解

3.使用 @Order 控制執(zhí)行順序

作用:通過 @Order 或?qū)崿F(xiàn) Ordered 接口控制多個(gè)啟動(dòng)任務(wù)的執(zhí)行順序。

示例:在 ApplicationRunner 中使用 @Order

import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
@Component
@Order(1)  // 數(shù)值越小優(yōu)先級越高
public class FirstRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) {
        System.out.println("第一個(gè)執(zhí)行的 Runner");
    }
}
 
@Component
@Order(2)
public class SecondRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) {
        System.out.println("第二個(gè)執(zhí)行的 Runner");
    }
}

到此這篇關(guān)于SpringBoot啟動(dòng)后自動(dòng)執(zhí)行方法的各種方式對比的文章就介紹到這了,更多相關(guān)SpringBoot啟動(dòng)自動(dòng)執(zhí)行方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 配置pom.xml用maven打包java工程的方法(推薦)

    配置pom.xml用maven打包java工程的方法(推薦)

    下面小編就為大家?guī)硪黄渲胮om.xml用maven打包java工程的方法(推薦)。小編覺得挺不錯(cuò)的, 現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • 淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系

    淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系

    這篇文章主要介紹了淺談Java自定義類加載器及JVM自帶的類加載器之間的交互關(guān)系,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • @JsonFormat處理LocalDateTime失效的問題

    @JsonFormat處理LocalDateTime失效的問題

    這篇文章主要介紹了關(guān)于@JsonFormat處理LocalDateTime失效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • java利用SMB讀取遠(yuǎn)程文件的方法

    java利用SMB讀取遠(yuǎn)程文件的方法

    這篇文章主要為大家詳細(xì)介紹了java利用SMB讀取遠(yuǎn)程文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • SpringBoot創(chuàng)建Docker鏡像的方法步驟

    SpringBoot創(chuàng)建Docker鏡像的方法步驟

    這篇文章主要介紹了SpringBoot創(chuàng)建Docker鏡像的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Jenkins的安裝配置詳解

    Jenkins的安裝配置詳解

    這篇文章主要介紹了Jenkins的安裝配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案

    idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案

    JSON(JavaScript Object Notation,JS對象簡譜)是一種輕量級的數(shù)據(jù)交換格式,這篇文章主要介紹了idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案,感興趣的朋友一起看看吧
    2023-11-11
  • Java中String類(字符串操作)的10個(gè)常見問題和解決方法

    Java中String類(字符串操作)的10個(gè)常見問題和解決方法

    這篇文章主要介紹了Java中String類(字符串)操作的10個(gè)常見問題,需要的朋友可以參考下
    2014-04-04
  • Hadoop上Data Locality的詳解

    Hadoop上Data Locality的詳解

    這篇文章主要介紹了 Hadoop上Data Locality的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • JDK1.7的ConcurrentHashMap源碼解析

    JDK1.7的ConcurrentHashMap源碼解析

    這篇文章主要介紹了JDK1.7的ConcurrentHashMap源碼解析,HashMap是非線程安全的,而HashTable是線程安全的,但是HashTable實(shí)現(xiàn)同步的方法比較暴力,即在所有的方法體上添加synchronized關(guān)鍵字,需要的朋友可以參考下
    2023-12-12

最新評論