SpringBoot啟動后立即執(zhí)行的幾種方法小結
在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,如我們需要去初始化數據到redis緩存,或者啟動后讀取相應的字典配置等,這篇文章主要聊聊實現立即執(zhí)行的幾種方法。
一、CommandLineRunner和ApplicationRunner
這兩者的實現方法一樣,都是去繼承相應的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項目中最常用的,比較靈活,有多個CommandLineRunner或ApplicationRunner實現類時可以通過@Order注解或實現Ordered接口重寫getOrder方法實現按指定順序執(zhí)行
1. CommandLineRunner的實現
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的實現
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實現類時指定順序執(zhí)行
通過@Order注解指定,數字越小越先執(zhí)行
@Component @Order(1) //通過order注解指定 public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner方法執(zhí)行"); } }
通過實現Ordered接口并重寫getOrder方法實現,數字越小越先執(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的不同
這兩者的不同其實就是run方法中所接收的參數類型不同,CommandLineRunner是對我們啟動jar包時所傳參數不進行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數,通過這個類型可以更方便的判斷某些參數是否存在之類的。
二、JDK所提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會在服務器加載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í)行"); ? ? } }
這些方法也都可以實現在啟動項目后立即執(zhí)行,但是不太常用。
到此這篇關于SpringBoot啟動后立即執(zhí)行的幾種方法小結的文章就介紹到這了,更多相關SpringBoot 啟動后執(zhí)行方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一文講透為什么遍歷LinkedList要用增強型for循環(huán)
這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04springboot的application.yml配置port不生效的解決方案
這篇文章主要介紹了springboot的application.yml配置port不生效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot整合Druid實現SQL監(jiān)控和數據庫密碼加密
Druid連接池是阿里巴巴開源的數據庫連接池項目,Druid連接池為監(jiān)控而生,內置強大的監(jiān)控功能,監(jiān)控特性不影響性能,本文給大家介紹了SpringBoot整合Druid實現SQL監(jiān)控和數據庫密碼加密,文中有相關的代碼示例供大家參考,需要的朋友可以參考下2024-06-06