SpringBoot實現(xiàn)啟動項目后立即執(zhí)行的方法總結(jié)
在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存,或者啟動后讀取相應(yīng)的字典配置等,這篇文章主要聊聊實現(xiàn)立即執(zhí)行的幾種方法。
一、CommandLineRunner和ApplicationRunner
這兩者的實現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項目中最常用的,比較靈活,有多個
CommandLineRunner或ApplicationRunner實現(xiàn)類時可以通過@Order注解或?qū)崿F(xiàn)Ordered接口重寫getOrder方法實現(xiàn)按指定順序執(zhí)行
1. CommandLineRunner的實現(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的實現(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. 多個CommandLineRunner或ApplicationRunner實現(xiàn)類時指定順序執(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í)行"); } }
通過實現(xiàn)Ordered接口并重寫getOrder方法實現(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的不同
這兩者的不同其實就是run方法中所接收的參數(shù)類型不同,CommandLineRunner是對我們啟動jar包時所傳參數(shù)不進行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過這個類型可以更方便的判斷某些參數(shù)是否存在之類的。
二、JDK所提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會在服務(wù)器加載Servlet的時候運行。也可以在一個類中寫多個方法并添加這個注解。
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(會執(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í)行"); } }
這些方法也都可以實現(xiàn)在啟動項目后立即執(zhí)行,但是不太常用。
到此這篇關(guān)于SpringBoot實現(xiàn)啟動項目后立即執(zhí)行的方法總結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot項目啟動后執(zhí)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot參數(shù)校驗及分組校驗的使用教程
在日常的開發(fā)中,參數(shù)校驗是非常重要的一個環(huán)節(jié),嚴格參數(shù)校驗會減少很多出bug的概率,增加接口的安全性,下面這篇文章主要給大家介紹了關(guān)于Spring Boot參數(shù)校驗及分組校驗使用的相關(guān)資料,需要的朋友可以參考下2021-08-08JAVA實現(xiàn)sm3加密簽名以及防止重復(fù)攻擊
這篇文章主要給大家介紹了關(guān)于JAVA實現(xiàn)sm3加密簽名以及防止重復(fù)攻擊的相關(guān)資料,SM3是簽名算法,和MD5一樣(對于應(yīng)用層來說),SM4是對稱加密算法,和AES一樣(對于應(yīng)用層來說),需要的朋友可以參考下2023-10-10java8學(xué)習(xí)教程之lambda表達式的使用方法
Java8最值得學(xué)習(xí)的特性就是Lambda表達式,下面這篇文章主要給大家介紹了關(guān)于java8學(xué)習(xí)教程之lambda表達式使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09Java Web檢查用戶登錄狀態(tài)(防止用戶訪問到非法頁面)
一般javaweb網(wǎng)站都有用戶登錄,而有一些操作必須用戶登錄才能進行,本文主要介紹了Java Web檢查用戶登錄狀態(tài),具有一定的參考價值,感興趣的可以了解一下2023-09-09kotlin java 混合代碼 maven 打包實現(xiàn)
這篇文章主要介紹了kotlin java 混合代碼 maven 打包實現(xiàn),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Java優(yōu)化for循環(huán)嵌套的高效率方法
這篇文章主要介紹了Java優(yōu)化for循環(huán)嵌套的高效率方法,幫助大家更好的提升java程序性能,感興趣的朋友可以了解下2020-09-09