SpringBoot啟動后立即執(zhí)行的幾種方法小結(jié)
在項(xiàng)目開發(fā)中某些場景必須要用到啟動項(xiàng)目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存,或者啟動后讀取相應(yīng)的字典配置等,這篇文章主要聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法。
一、CommandLineRunner和ApplicationRunner
這兩者的實(shí)現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項(xiàng)目中最常用的,比較靈活,有多個CommandLineRunner或ApplicationRunner實(shí)現(xiàn)類時可以通過@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. 多個CommandLineRunner或ApplicationRunner實(shí)現(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í)行");
}
}通過實(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是對我們啟動jar包時所傳參數(shù)不進(jìn)行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過這個類型可以更方便的判斷某些參數(shù)是否存在之類的。
二、JDK所提供的@PostConstruct
@PostConstruct是JDK所提供的注解,使用該注解的方法會在服務(wù)器加載Servlet的時候運(yùn)行。也可以在一個類中寫多個方法并添加這個注解。
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í)行");
? ? }
}這些方法也都可以實(shí)現(xiàn)在啟動項(xiàng)目后立即執(zhí)行,但是不太常用。
到此這篇關(guān)于SpringBoot啟動后立即執(zhí)行的幾種方法小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot 啟動后執(zhí)行方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)學(xué)生信息錄入界面
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生信息錄入界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Javaweb-HttpServletResponse的sendRedirectch重定向方式
這篇文章主要介紹了Javaweb-HttpServletResponse的sendRedirectch重定向方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
apollo更改配置刷新@ConfigurationProperties配置類
這篇文章主要為大家介紹了apollo更改配置刷新@ConfigurationProperties配置類示例解析,apollo更改配置刷新@ConfigurationProperties配置類2023-04-04
AgileBoot?項(xiàng)目內(nèi)統(tǒng)一的錯誤碼設(shè)計(jì)分析
這篇文章主要為大家介紹了AgileBoot?項(xiàng)目內(nèi)統(tǒng)一的錯誤碼設(shè)計(jì)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
基于Java實(shí)現(xiàn)的Dijkstra算法示例
這篇文章主要介紹了基于Java實(shí)現(xiàn)的Dijkstra算法示例,一個比較典型的算法示例,需要的朋友可以參考下2014-07-07
SpringBoot實(shí)現(xiàn)阿里云短信發(fā)送的示例代碼
這篇文章主要為大家介紹了如何利用SpringBoot實(shí)現(xiàn)阿里云短信發(fā)送,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-04-04

