Spring?Boot在啟動(dòng)時(shí)執(zhí)行一次的功能實(shí)現(xiàn)
方法一:@PostConstruct
此方法可能是最常用的
可以使用Spring Boot的@PostConstruct注解來(lái)實(shí)現(xiàn)在啟動(dòng)時(shí)執(zhí)行一次的功能。@PostConstruct注解標(biāo)記的方法會(huì)在Bean初始化完成后自動(dòng)調(diào)用,可以在該方法中執(zhí)行只需要在啟動(dòng)時(shí)執(zhí)行一次的操作。
如果想在生成對(duì)象時(shí)完成某些初始化操作,而偏偏這些初始化操作又依賴于依賴注入,那么就無(wú)法在構(gòu)造函數(shù)中實(shí)現(xiàn)。為此,可以使用@PostConstruct注解一個(gè)方法來(lái)完成初始化,@PostConstruct注解的方法將會(huì)在依賴注入完成后被自動(dòng)調(diào)用。
Constructor >> @Autowired >> @PostConstruct
例如,在一個(gè)Spring Boot應(yīng)用程序的啟動(dòng)類中添加一個(gè)@PostConstruct注解標(biāo)記的方法:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; @SpringBootApplication public class MyApplication { @PostConstruct public void init() { // 在這里執(zhí)行僅需在啟動(dòng)時(shí)執(zhí)行一次的操作 } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
在以上示例代碼中,init()方法被標(biāo)記為@PostConstruct注解,表示它會(huì)在MyApplication Bean初始化完成后自動(dòng)調(diào)用。在init()方法中可以執(zhí)行只需要在啟動(dòng)時(shí)執(zhí)行一次的操作,例如初始化一些數(shù)據(jù)、建立數(shù)據(jù)庫(kù)連接等。
方法二:使用Spring Boot提供的CommandLineRunner接口或ApplicationRunner接口
此方法已經(jīng)在項(xiàng)目中實(shí)踐使用ok。
除了@PostConstruct注解,還可以使用Spring Boot提供的CommandLineRunner接口或ApplicationRunner接口來(lái)實(shí)現(xiàn)在啟動(dòng)時(shí)執(zhí)行一次的功能。
這兩個(gè)接口都有一個(gè)run()方法,在應(yīng)用程序啟動(dòng)后會(huì)被自動(dòng)調(diào)用。需要在該方法中實(shí)現(xiàn)需要在啟動(dòng)時(shí)執(zhí)行的操作,例如初始化數(shù)據(jù)、開啟定時(shí)任務(wù)等。
如果需要多個(gè)操作在啟動(dòng)時(shí)執(zhí)行,可以定義多個(gè)實(shí)現(xiàn)了CommandLineRunner或ApplicationRunner接口的Bean,并通過(guò)@Order注解指定它們的執(zhí)行順序。
示例代碼如下:
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Component @Order(1) // 可以通過(guò)@Order注解指定執(zhí)行順序,數(shù)字越小越先執(zhí)行 public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 在這里執(zhí)行啟動(dòng)時(shí)需要執(zhí)行的操作 } } @Component @Order(2) public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 在這里執(zhí)行啟動(dòng)時(shí)需要執(zhí)行的操作 } }
以上示例代碼定義了兩個(gè)Bean,分別是實(shí)現(xiàn)CommandLineRunner接口的MyCommandLineRunner和實(shí)現(xiàn)ApplicationRunner接口的MyApplicationRunner。它們的run()方法會(huì)在應(yīng)用程序啟動(dòng)后自動(dòng)調(diào)用,可以在這里實(shí)現(xiàn)需要在啟動(dòng)時(shí)執(zhí)行的操作。其中,@Order注解用于指定它們的執(zhí)行順序,數(shù)字越小越先執(zhí)行。
方法三:使用Spring Boot提供的ApplicationListener接口
此方法暫未實(shí)踐
還可以使用Spring Boot提供的ApplicationListener接口來(lái)實(shí)現(xiàn)在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行一次的功能。這個(gè)接口定義了監(jiān)聽Spring Boot應(yīng)用程序事件的方法,當(dāng)應(yīng)用程序觸發(fā)相應(yīng)的事件時(shí),監(jiān)聽器會(huì)自動(dòng)調(diào)用相應(yīng)的方法進(jìn)行處理。
具體實(shí)現(xiàn)步驟如下:
創(chuàng)建一個(gè)實(shí)現(xiàn)ApplicationListener接口的類,例如MyApplicationListener。
實(shí)現(xiàn)onApplicationEvent()方法,在該方法中編寫需要在啟動(dòng)時(shí)執(zhí)行的操作,例如初始化數(shù)據(jù)、建立數(shù)據(jù)庫(kù)連接等。
通過(guò)@Component注解或@Bean注解將MyApplicationListener注冊(cè)成Spring Bean。
示例代碼如下:
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) { // 在這里執(zhí)行啟動(dòng)時(shí)需要執(zhí)行的操作 } }
以上示例代碼創(chuàng)建了一個(gè)名為MyApplicationListener的Bean,并實(shí)現(xiàn)了ApplicationListener接口,用于監(jiān)聽ApplicationReadyEvent事件。在onApplicationEvent()方法中編寫需要在啟動(dòng)時(shí)執(zhí)行的操作。最后通過(guò)@Component注解將MyApplicationListener注冊(cè)成Spring Bean。
當(dāng)應(yīng)用程序啟動(dòng)完成后,MyApplicationListener會(huì)自動(dòng)監(jiān)聽到ApplicationReadyEvent事件并執(zhí)行其中的代碼??梢栽谶@里實(shí)現(xiàn)需要在啟動(dòng)時(shí)執(zhí)行一次的操作,確保其只在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行一次。
總結(jié)
到此這篇關(guān)于Spring Boot在啟動(dòng)時(shí)執(zhí)行一次的功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot啟動(dòng)時(shí)執(zhí)行一次內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 實(shí)戰(zhàn)項(xiàng)目之小說(shuō)在線閱讀系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)前臺(tái)閱讀后臺(tái)管理的小說(shuō)在線閱讀系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11Java如何實(shí)現(xiàn)實(shí)體類轉(zhuǎn)Map、Map轉(zhuǎn)實(shí)體類
這篇文章主要介紹了Java 實(shí)現(xiàn)實(shí)體類轉(zhuǎn)Map、Map轉(zhuǎn)實(shí)體類的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08關(guān)于synchronized有趣的同步問(wèn)題
今天小編就為大家分享一篇關(guān)于關(guān)于synchronized有趣的同步問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01詳解Java編程中static關(guān)鍵字和final關(guān)鍵字的使用
這篇文章主要介紹了詳解Java編程中static關(guān)鍵字和final關(guān)鍵字的使用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09SpringBoot整合Shiro兩種方式(總結(jié))
這篇文章主要介紹了SpringBoot整合Shiro兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06classloader類加載器_基于java類的加載方式詳解
下面小編就為大家?guī)?lái)一篇classloader類加載器_基于java類的加載方式詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10SpringBoot中@Configuration和@Bean和@Component相同點(diǎn)詳解
這篇文章主要介紹了SpringBoot中@Configuration和@Bean和@Component相同點(diǎn)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04