最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot啟動后立即執(zhí)行的幾種方法小結(jié)

 更新時間:2023年05月28日 12:54:32   作者:JK凱  
在項(xiàng)目開發(fā)中某些場景必須要用到啟動項(xiàng)目后立即執(zhí)行方式的功能,本文主要介紹了SpringBoot啟動后立即執(zhí)行的幾種方法小結(jié),具有一定的參考價(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)文章

最新評論

罗田县| 广州市| 城步| 沙田区| 长寿区| 梁河县| 恩平市| 介休市| 阳新县| 云南省| 宜兰市| 阿拉善右旗| 武宁县| 井研县| 呼图壁县| 洛浦县| 锦州市| 柳州市| 苍梧县| 彭水| 寿阳县| 双江| 荥阳市| 基隆市| 龙山县| 桂平市| 农安县| 宁陕县| 罗源县| 汉沽区| 长葛市| 北安市| 孝昌县| 湟中县| 贺州市| 县级市| 潜江市| 桂东县| 鸡西市| 连城县| 汾西县|