前言
目前開發的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(…)的應用參數。
參考資料:
Interface ApplicationArguments
CommandLineRunner或者ApplicationRunner接口
Order註解
當有多個類實現了CommandLineRunner和ApplicationRunner接口時,可以通過在類上添加@Order註解來設定運行順序。
代碼測試
爲了測試啓動時運行的效果和順序,編寫幾個測試代碼來運行看看。
TestPostConstruct
@Component
publicclass TestPostConstruct {
static {
System.out.println("static");
}
publicTestPostConstruct() {
System.out.println("constructer");
}
@PostConstruct
publicvoidinit() {
System.out.println("PostConstruct");
}
}
TestApplicationRunner
@Component@Order(1)
publicclassTestApplicationRunnerimplementsApplicationRunner{@Overridepublicvoidrun(ApplicationArguments applicationArguments) throws Exception {
System.out.println("order1:TestApplicationRunner");
}
}
TestCommandLineRunner
@Component@Order(2)
publicclassTestCommandLineRunnerimplementsCommandLineRunner {@Overridepublicvoidrun(String... strings) throws Exception {
System.out.println("order2:TestCommandLineRunner");
}
}
執行結果
總結
Spring應用啓動過程中,肯定是要自動掃描有@Component註解的類,加載類並初始化對象進行自動注入。加載類時首先要執行static靜態代碼塊中的代碼,之後再初始化對象時會執行構造方法。在對象注入完成後,調用帶有@PostConstruct註解的方法。當容器啓動成功後,再根據@Order註解的順序調用CommandLineRunner和ApplicationRunner接口類中的run方法。
因此,加載順序爲:
static > constructer > @PostConstruct > CommandLineRunner 和 ApplicationRunner