欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式

 更新時(shí)間:2023年09月12日 11:39:13   作者:無(wú)夢(mèng).  
在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,本文主要聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下

在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,如我們需要去初始化數(shù)據(jù)到redis緩存、設(shè)置策略工廠,或者啟動(dòng)后讀取相應(yīng)的配置等,主要聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法。

一、CommandLineRunner

實(shí)現(xiàn)CommandLineRunner接口 然后在run方法里面調(diào)用需要調(diào)用的方法即可,好處是方法執(zhí)行時(shí),項(xiàng)目已經(jīng)初始化完畢,是可以正常提供服務(wù)的。

同時(shí)該方法也可以接受參數(shù),可以根據(jù)項(xiàng)目啟動(dòng)時(shí): java -jar demo.jar arg1 arg2 arg3 傳入的參數(shù)進(jìn)行一些處理。

@Component
public class CommandLineRunnerImpl implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(Arrays.toString(args));
}
}

二、 ApplicationRunner

這兩者的實(shí)現(xiàn)方法一樣,都是去繼承相應(yīng)的接口然后重寫run方法即可,也都是SpringBoot框架所提供給我們的接口,也是項(xiàng)目中最常用的,比較靈活,有多個(gè)CommandLineRunner或ApplicationRunner實(shí)現(xiàn)類時(shí)可以通過(guò)@Order注解或?qū)崿F(xiàn)Ordered接口重寫getOrder方法實(shí)現(xiàn)按指定順序執(zhí)行。

@Order(1) //通過(guò)order注解指定執(zhí)行順序
@Component
public class CommandLineRunnerInit implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunner 啟動(dòng)后執(zhí)行方法...");
    }
}

ApplicationRunner 通過(guò)order注解指定執(zhí)行順序

@Order(2) //通過(guò)order注解指定執(zhí)行順序
@Component
public class ApplicationRunnerInit implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner 啟動(dòng)后執(zhí)行方法...");
    }
}

ApplicationRunner 通過(guò)實(shí)現(xiàn) Ordered 指定執(zhí)行順序

@Component
// 通過(guò)實(shí)現(xiàn) Ordered 指定執(zhí)行順序
public class ApplicationRunnerInit implements ApplicationRunner, Ordered {
? ? @Override
? ? public void run(ApplicationArguments args) throws Exception {
? ? ? ? System.out.println("ApplicationRunner 啟動(dòng)后執(zhí)行方法...");
? ? }
? ? @Override
? ? public int getOrder() {
? ? ? ?return 0;
? ? }
}

這兩者的不同其實(shí)就是run方法中所接收的參數(shù)類型不同,CommandLineRunner是對(duì)我們啟動(dòng)jar包時(shí)所傳參數(shù)不進(jìn)行處理,原樣返回String類型給你,ApplicationRunner是封裝成了ApplicationArguments參數(shù),通過(guò)這個(gè)類型可以更方便的判斷某些參數(shù)是否存在之類的。

三、JDK提供的@PostConstruct

@PostConstruct是JDK所提供的注解,使用該注解的方法會(huì)在服務(wù)器加載Servlet的時(shí)候運(yùn)行。也可以在一個(gè)類中寫多個(gè)方法并添加這個(gè)注解。

@Component
public class PostConstructTest {
? ? @PostConstruct
? ? public void inti1() {
? ? ? ? System.out.println("@PostConstruct 方法1執(zhí)行...");
? ? }
? ? @PostConstruct
? ? public void inti2() {
? ? ? ? System.out.println("@PostConstruct 方法2執(zhí)行...");
? ? }
}

三、其他方法

3.1 ApplicationContextAware

ApplicationContextAware 一般被用來(lái)獲取applicationContext上下文信息,并且也可以啟動(dòng)時(shí)被執(zhí)行

@Component
public class ApplicationContextAwareTest implements ApplicationContextAware {
? ? private ApplicationContext applicationContext;
? ? @Override
? ? public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
? ? ? ? this.applicationContext = applicationContext;
? ? ? ? System.out.println("ApplicationContextAware 方法執(zhí)行...");
? ? }
}

3.2 ApplicationListener

ApplicationListener 事件監(jiān)聽,啟動(dòng)會(huì)執(zhí)行多個(gè)監(jiān)聽器【會(huì)多次執(zhí)行】,具體可了解Springboot服務(wù)啟動(dòng)過(guò)程。

@Component
public class ApplicationListenerTest implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("ApplicationListener 方法執(zhí)行...");
    }
}

3.3 InitializingBean

InitializingBean 初始化啟動(dòng)后方法執(zhí)行

@Component
public class InitializingBeanInit implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean 方法執(zhí)行...");
    }
}

四、總結(jié)

以上多種初始執(zhí)行方法日志輸出為:

ApplicationContextAware 方法執(zhí)行...
InitializingBean 方法執(zhí)行...
    
@PostConstruct 方法1執(zhí)行...
@PostConstruct 方法2執(zhí)行...

ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...
    
ApplicationRunner 啟動(dòng)后執(zhí)行方法...
CommandLineRunner 啟動(dòng)后執(zhí)行方法...
    
ApplicationListener 方法執(zhí)行...
ApplicationListener 方法執(zhí)行...

存在多個(gè)ApplicationRunner、CommandLineRunner可以通過(guò)order指定其執(zhí)行順序,但其執(zhí)行順序沒(méi)有ApplicationContextAware等高。但一般生產(chǎn)中使用ApplicationRunner、CommandLineRunner,因?yàn)槠涓屿`活。

到此這篇關(guān)于詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式的文章就介紹到這了,更多相關(guān)SpringBoot啟動(dòng)后執(zhí)行方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論