SpringBoot啟動(dòng)后立即執(zhí)行的幾種方法小結(jié)
在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存,或者啟動(dòng)后讀取相應(yīng)的字典配置等,這篇文章主要聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法。
一、CommandLineRunner和ApplicationRunner
這兩者的實(shí)現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項(xiàng)目中最常用的,比較靈活,有多個(gè)CommandLineRunner或ApplicationRunner實(shí)現(xiàn)類時(shí)可以通過@Order注解或?qū)崿F(xiàn)Ordered接口重寫getOrder方法實(shí)現(xiàn)按指定順序執(zhí)行
1. CommandLineRunner的實(shí)現(xiàn)
import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class CommandLineRunnerImpl implements CommandLineRunner { ? ? @Override ? ? public void run(String... args) throws Exception { ? ? ? ? System.out.println("CommandLineRunnerImpl方法執(zhí)行"); ? ? } }
2. ApplicationRunner的實(shí)現(xiàn)
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class ApplicationRunnerImpl implements ApplicationRunner { ? ? @Override ? ? public void run(ApplicationArguments args) throws Exception { ? ? ? ? System.out.println("ApplicationRunner方法執(zhí)行"); ? ? }
3. 多個(gè)CommandLineRunner或ApplicationRunner實(shí)現(xiàn)類時(shí)指定順序執(zhí)行
通過@Order注解指定,數(shù)字越小越先執(zhí)行
@Component @Order(1) //通過order注解指定 public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner方法執(zhí)行"); } }
通過實(shí)現(xiàn)Ordered接口并重寫getOrder方法實(shí)現(xiàn),數(shù)字越小越先執(zhí)行
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; @Component public class ApplicationRunnerImpl implements ApplicationRunner, Ordered { ? ? @Override ? ? public void run(ApplicationArguments args) throws Exception { ? ? ? ? System.out.println("ApplicationRunner方法執(zhí)行"); ? ? } ? ? @Override ? ? public int getOrder() { ? ? ? ? return 1; ? ? } }
CommandLineRunner或ApplicationRunner的不同
這兩者的不同其實(shí)就是run方法中所接收的參數(shù)類型不同,CommandLineRunner是對(duì)我們啟動(dòng)jar包時(shí)所傳參數(shù)不進(jìn)行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過這個(gè)類型可以更方便的判斷某些參數(shù)是否存在之類的。
二、JDK所提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會(huì)在服務(wù)器加載Servlet的時(shí)候運(yùn)行。也可以在一個(gè)類中寫多個(gè)方法并添加這個(gè)注解。
import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class PostConstructTest { ? ? @PostConstruct ? ? public void start() { ? ? ? ? System.out.println("@PostConstruct方法執(zhí)行"); ? ? } ? ? @PostConstruct ? ? public void start01() { ? ? ? ? System.out.println("@PostConstruct1111方法執(zhí)行"); ? ? } }
三、其他方法(不常用)
1. ApplicationContextAware
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextAwareImpl implements ApplicationContextAware { ? ? @Override ? ? public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ? ? ? ? System.out.println("ApplicationContextAwareImpl方法執(zhí)行"); ? ? } }
2. ApplicationListener(會(huì)執(zhí)行多次)
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class ApplicationListenerImpl implements ApplicationListener { ? ? @Override ? ? public void onApplicationEvent(ApplicationEvent event) { ? ? ? ? System.out.println("ApplicationListenerImpl方法執(zhí)行"); ? ? } }
3. InitializingBean
import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @Component public class InitializingBeanImpl implements InitializingBean { ? ? @Override ? ? public void afterPropertiesSet() throws Exception { ? ? ? ? System.out.println("InitializingBeanImpl方法執(zhí)行"); ? ? } }
這些方法也都可以實(shí)現(xiàn)在啟動(dòng)項(xiàng)目后立即執(zhí)行,但是不太常用。
到此這篇關(guān)于SpringBoot啟動(dòng)后立即執(zhí)行的幾種方法小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot 啟動(dòng)后執(zhí)行方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(53)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08一文講透為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)
這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04springboot的application.yml配置port不生效的解決方案
這篇文章主要介紹了springboot的application.yml配置port不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Java中instanceof關(guān)鍵字實(shí)例講解
大家好,本篇文章主要講的是Java中instanceof關(guān)鍵字實(shí)例講解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Java,C#使用二進(jìn)制序列化、反序列化操作數(shù)據(jù)
這篇文章主要介紹了Java,C#使用二進(jìn)制序列化、反序列化操作數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2014-10-10Java實(shí)現(xiàn)合并word文檔的示例代碼
在做項(xiàng)目中,經(jīng)常會(huì)遇到一種情況,需要將一個(gè)小word文檔的內(nèi)容插入到一個(gè)大word(主文檔)中。本文就為大家準(zhǔn)備了Java實(shí)現(xiàn)合并word文檔的方法,需要的可以參考一下2022-08-08SpringBoot整合Druid實(shí)現(xiàn)SQL監(jiān)控和數(shù)據(jù)庫密碼加密
Druid連接池是阿里巴巴開源的數(shù)據(jù)庫連接池項(xiàng)目,Druid連接池為監(jiān)控而生,內(nèi)置強(qiáng)大的監(jiān)控功能,監(jiān)控特性不影響性能,本文給大家介紹了SpringBoot整合Druid實(shí)現(xiàn)SQL監(jiān)控和數(shù)據(jù)庫密碼加密,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-06-06SpringBoot中動(dòng)態(tài)數(shù)據(jù)源配置與使用詳解
在現(xiàn)代應(yīng)用中,處理多數(shù)據(jù)源是常見的需求,在 Spring Boot 中,這樣的需求可以通過動(dòng)態(tài)數(shù)據(jù)源來輕松實(shí)現(xiàn),本篇博客將詳細(xì)介紹如何在 Spring Boot 中配置和使用動(dòng)態(tài)數(shù)據(jù)源,并演示如何切換到指定的數(shù)據(jù)源,需要的朋友可以參考下2024-10-10