SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式
前言
目前開(kāi)發(fā)的SpringBoot項(xiàng)目在啟動(dòng)的時(shí)候需要預(yù)加載一些資源。而如何實(shí)現(xiàn)啟動(dòng)過(guò)程中執(zhí)行代碼,或啟動(dòng)成功后執(zhí)行,是有很多種方式可以選擇,我們可以在static代碼塊中實(shí)現(xiàn),也可以在構(gòu)造方法里實(shí)現(xiàn),也可以使用@PostConstruct注解實(shí)現(xiàn)。
當(dāng)然也可以去實(shí)現(xiàn)Spring的ApplicationRunner與CommandLineRunner接口去實(shí)現(xiàn)啟動(dòng)后運(yùn)行的功能。在這里整理一下,在這些位置執(zhí)行的區(qū)別以及加載順序。
java自身的啟動(dòng)時(shí)加載方式
static代碼塊
static靜態(tài)代碼塊,在類加載的時(shí)候即自動(dòng)執(zhí)行。
構(gòu)造方法
在對(duì)象初始化時(shí)執(zhí)行。執(zhí)行順序在static靜態(tài)代碼塊之后。
Spring啟動(dòng)時(shí)加載方式
@PostConstruct注解
PostConstruct注解使用在方法上,這個(gè)方法在對(duì)象依賴注入初始化之后執(zhí)行。
ApplicationRunner和CommandLineRunner
SpringBoot提供了兩個(gè)接口來(lái)實(shí)現(xiàn)Spring容器啟動(dòng)完成后執(zhí)行的功能,兩個(gè)接口分別為CommandLineRunner和ApplicationRunner。
這兩個(gè)接口需要實(shí)現(xiàn)一個(gè)run方法,將代碼在run中實(shí)現(xiàn)即可。這兩個(gè)接口功能基本一致,其區(qū)別在于run方法的入?yún)?。ApplicationRunner的run方法入?yún)锳pplicationArguments,為CommandLineRunner的run方法入?yún)镾tring數(shù)組。
何為ApplicationArguments
官方文檔解釋為:
”Provides access to the arguments that were used to run a SpringApplication.
在Spring應(yīng)用運(yùn)行時(shí)使用的訪問(wèn)應(yīng)用參數(shù)。即我們可以獲取到SpringApplication.run(…)的應(yīng)用參數(shù)。
Order注解
當(dāng)有多個(gè)類實(shí)現(xiàn)了CommandLineRunner和ApplicationRunner接口時(shí),可以通過(guò)在類上添加@Order注解來(lái)設(shè)定運(yùn)行順序。
代碼測(cè)試
為了測(cè)試啟動(dòng)時(shí)運(yùn)行的效果和順序,編寫幾個(gè)測(cè)試代碼來(lái)運(yùn)行看看。
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");
}
}執(zhí)行結(jié)果

總結(jié)
Spring應(yīng)用啟動(dòng)過(guò)程中,肯定是要自動(dòng)掃描有@Component注解的類,加載類并初始化對(duì)象進(jìn)行自動(dòng)注入。加載類時(shí)首先要執(zhí)行static靜態(tài)代碼塊中的代碼,之后再初始化對(duì)象時(shí)會(huì)執(zhí)行構(gòu)造方法。
在對(duì)象注入完成后,調(diào)用帶有@PostConstruct注解的方法。當(dāng)容器啟動(dòng)成功后,再根據(jù)@Order注解的順序調(diào)用CommandLineRunner和ApplicationRunner接口類中的run方法。
因此,加載順序?yàn)閟tatic>constructer>@PostConstruct>CommandLineRunner和ApplicationRunner.
到此這篇關(guān)于SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)SpringBoot啟動(dòng)自動(dòng)執(zhí)行代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot工程啟動(dòng)時(shí)自動(dòng)執(zhí)行任務(wù)實(shí)現(xiàn)方式
- SpringBoot啟動(dòng)后自動(dòng)執(zhí)行方法的各種方式對(duì)比
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行特定代碼的完整指南
- SpringBoot啟動(dòng)后自動(dòng)執(zhí)行初始化任務(wù)的五種方法
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行指定方法的幾種實(shí)現(xiàn)方式
- springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法
- SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行sql腳本的方法步驟
- springBoot啟動(dòng)時(shí)讓方法自動(dòng)執(zhí)行的幾種實(shí)現(xiàn)方式
- SpringBoot 啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種方式講解
相關(guān)文章
SpringBoot項(xiàng)目構(gòu)建Maven標(biāo)簽及屬性用法詳解
在?Spring?Boot?項(xiàng)目中,Maven?是最常用的構(gòu)建工具之一,本文將詳細(xì)介紹?Maven?依賴管理中的主要標(biāo)簽及其使用方法,幫助開(kāi)發(fā)者更好地理解和使用?Maven?構(gòu)建工具,感興趣的朋友跟隨小編一起看看吧2024-08-08
SpringBoot中整合Minio文件存儲(chǔ)的安裝部署過(guò)程
這篇文章主要介紹了SpringBoot整合Minio文件存儲(chǔ)的相關(guān)知識(shí),詳細(xì)介紹了Minio安裝部署過(guò)程,需要的朋友可以參考下2022-04-04
Java雜談之代碼重構(gòu)的方法多長(zhǎng)才算長(zhǎng)
關(guān)于代碼重構(gòu)的理解:在不改變軟件系統(tǒng)/模塊所具備的功能特性的前提下,遵循/利用某種規(guī)則,使其內(nèi)部結(jié)構(gòu)趨于完善。其在軟件生命周期中的價(jià)值體現(xiàn)主要在于可維護(hù)性和可擴(kuò)展性2021-10-10
Spring使用@Autowired為抽象父類注入依賴代碼實(shí)例
這篇文章主要介紹了Spring使用@Autowired為抽象父類注入依賴代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

