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

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

 更新時間:2023年05月28日 12:54:32   作者:JK凱  
在項目開發(fā)中某些場景必須要用到啟動項目后立即執(zhí)行方式的功能,本文主要介紹了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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java日常練習題,每天進步一點點(53)

    Java日常練習題,每天進步一點點(53)

    下面小編就為大家?guī)硪黄狫ava基礎的幾道練習題(分享)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • 一文講透為什么遍歷LinkedList要用增強型for循環(huán)

    一文講透為什么遍歷LinkedList要用增強型for循環(huán)

    這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • springboot的application.yml配置port不生效的解決方案

    springboot的application.yml配置port不生效的解決方案

    這篇文章主要介紹了springboot的application.yml配置port不生效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java中instanceof關鍵字實例講解

    Java中instanceof關鍵字實例講解

    大家好,本篇文章主要講的是Java中instanceof關鍵字實例講解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Java,C#使用二進制序列化、反序列化操作數據

    Java,C#使用二進制序列化、反序列化操作數據

    這篇文章主要介紹了Java,C#使用二進制序列化、反序列化操作數據的相關資料,需要的朋友可以參考下
    2014-10-10
  • Spring Boot 2.X優(yōu)雅的解決跨域問題

    Spring Boot 2.X優(yōu)雅的解決跨域問題

    這篇文章主要給大家介紹了關于Spring Boot 2.X如何優(yōu)雅的解決跨域問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot 2.X具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • Java聊天室之解決連接超時問題

    Java聊天室之解決連接超時問題

    這篇文章主要為大家詳細介紹了Java簡易聊天室之解決連接超時問題的方法,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以了解一下
    2022-10-10
  • Java實現合并word文檔的示例代碼

    Java實現合并word文檔的示例代碼

    在做項目中,經常會遇到一種情況,需要將一個小word文檔的內容插入到一個大word(主文檔)中。本文就為大家準備了Java實現合并word文檔的方法,需要的可以參考一下
    2022-08-08
  • SpringBoot整合Druid實現SQL監(jiān)控和數據庫密碼加密

    SpringBoot整合Druid實現SQL監(jiān)控和數據庫密碼加密

    Druid連接池是阿里巴巴開源的數據庫連接池項目,Druid連接池為監(jiān)控而生,內置強大的監(jiān)控功能,監(jiān)控特性不影響性能,本文給大家介紹了SpringBoot整合Druid實現SQL監(jiān)控和數據庫密碼加密,文中有相關的代碼示例供大家參考,需要的朋友可以參考下
    2024-06-06
  • SpringBoot中動態(tài)數據源配置與使用詳解

    SpringBoot中動態(tài)數據源配置與使用詳解

    在現代應用中,處理多數據源是常見的需求,在 Spring Boot 中,這樣的需求可以通過動態(tài)數據源來輕松實現,本篇博客將詳細介紹如何在 Spring Boot 中配置和使用動態(tài)數據源,并演示如何切換到指定的數據源,需要的朋友可以參考下
    2024-10-10

最新評論