天天看點

SpringBoot 啟動時自動執行代碼的幾種方式,還有誰不會?

作者:Java小老太

前言

目前開發的SpringBoot項目在啟動的時候需要預加載一些資源。而如何實作啟動過程中執行代碼,或啟動成功後執行,是有很多種方式可以選擇,我們可以在static代碼塊中實作,也可以在構造方法裡實作,也可以使用@PostConstruct注解實作。

當然也可以去實作Spring的ApplicationRunner與CommandLineRunner接口去實作啟動後運作的功能。在這裡整理一下,在這些位置執行的差別以及加載順序。

java自身的啟動時加載方式

static代碼塊

static靜态代碼塊,在類加載的時候即自動執行。

構造方法

在對象初始化時執行。執行順序在static靜态代碼塊之後。

Spring啟動時加載方式

@PostConstruct注解

PostConstruct注解使用在方法上,這個方法在對象依賴注入初始化之後執行。

ApplicationRunner和CommandLineRunner

SpringBoot提供了兩個接口來實作Spring容器啟動完成後執行的功能,兩個接口分别為CommandLineRunner和ApplicationRunner。

這兩個接口需要實作一個run方法,将代碼在run中實作即可。這兩個接口功能基本一緻,其差別在于run方法的入參。ApplicationRunner的run方法入參為ApplicationArguments,為CommandLineRunner的run方法入參為String數組。

何為ApplicationArguments

官方文檔解釋為:

Provides access to the arguments that were used to run a SpringApplication.

在Spring應用運作時使用的通路應用參數。即我們可以擷取到SpringApplication.run(…)的應用參數。

Order注解

當有多個類實作了CommandLineRunner和ApplicationRunner接口時,可以通過在類上添加@Order注解來設定運作順序。

代碼測試

為了測試啟動時運作的效果和順序,編寫幾個測試代碼來運作看看。

TestPostConstruct

@Component
public class TestPostConstruct {

    static {
        System.out.println("static");
    }
    public TestPostConstruct() {
        System.out.println("constructer");
    }

    @PostConstruct
    public void init() {
        System.out.println("PostConstruct");
    }
}
           

TestApplicationRunner

@Component
@Order(1)
public class TestApplicationRunner implements ApplicationRunner{
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        System.out.println("order1:TestApplicationRunner");
    }
}
           

TestCommandLineRunner

@Component
@Order(2)
public class TestCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... strings) throws Exception {
        System.out.println("order2:TestCommandLineRunner");
    }
}
           

執行結果

SpringBoot 啟動時自動執行代碼的幾種方式,還有誰不會?

圖檔

總結

Spring應用啟動過程中,肯定是要自動掃描有@Component注解的類,加載類并初始化對象進行自動注入。加載類時首先要執行static靜态代碼塊中的代碼,之後再初始化對象時會執行構造方法。

在對象注入完成後,調用帶有@PostConstruct注解的方法。當容器啟動成功後,再根據@Order注解的順序調用CommandLineRunner和ApplicationRunner接口類中的run方法。

是以,加載順序為static>constructer>@PostConstruct>CommandLineRunner和ApplicationRunner.