SpringBoot實(shí)現(xiàn)啟動(dòng)項(xià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; } }
4.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實(shí)現(xiàn)啟動(dòng)項(xiàng)目后立即執(zhí)行的方法總結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot項(xiàng)目啟動(dòng)后執(zhí)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- IntelliJ IDEA下SpringBoot如何指定某一個(gè)配置文件啟動(dòng)項(xiàng)目
- 詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式
- springboot+dubbo啟動(dòng)項(xiàng)目時(shí)報(bào)錯(cuò) zookeeper not connected的問題及解決方案
- IDEA下SpringBoot指定配置文件啟動(dòng)項(xiàng)目的全過程
- java?-jar命令及SpringBoot通過java?-jav啟動(dòng)項(xiàng)目的過程
- springboot 啟動(dòng)項(xiàng)目打印接口列表的實(shí)現(xiàn)
- idea沒有services窗口、沒有springboot啟動(dòng)項(xiàng)問題
相關(guān)文章
Spring Boot參數(shù)校驗(yàn)及分組校驗(yàn)的使用教程
在日常的開發(fā)中,參數(shù)校驗(yàn)是非常重要的一個(gè)環(huán)節(jié),嚴(yán)格參數(shù)校驗(yàn)會(huì)減少很多出bug的概率,增加接口的安全性,下面這篇文章主要給大家介紹了關(guān)于Spring Boot參數(shù)校驗(yàn)及分組校驗(yàn)使用的相關(guān)資料,需要的朋友可以參考下2021-08-08JAVA實(shí)現(xiàn)sm3加密簽名以及防止重復(fù)攻擊
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)sm3加密簽名以及防止重復(fù)攻擊的相關(guān)資料,SM3是簽名算法,和MD5一樣(對(duì)于應(yīng)用層來說),SM4是對(duì)稱加密算法,和AES一樣(對(duì)于應(yīng)用層來說),需要的朋友可以參考下2023-10-10java8學(xué)習(xí)教程之lambda表達(dá)式的使用方法
Java8最值得學(xué)習(xí)的特性就是Lambda表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于java8學(xué)習(xí)教程之lambda表達(dá)式使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09Java Web檢查用戶登錄狀態(tài)(防止用戶訪問到非法頁面)
一般javaweb網(wǎng)站都有用戶登錄,而有一些操作必須用戶登錄才能進(jìn)行,本文主要介紹了Java Web檢查用戶登錄狀態(tài),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09kotlin java 混合代碼 maven 打包實(shí)現(xiàn)
這篇文章主要介紹了kotlin java 混合代碼 maven 打包實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Java優(yōu)化for循環(huán)嵌套的高效率方法
這篇文章主要介紹了Java優(yōu)化for循環(huán)嵌套的高效率方法,幫助大家更好的提升java程序性能,感興趣的朋友可以了解下2020-09-09