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

SpringBoot中的ApplicationRunner與CommandLineRunner問(wèn)題

 更新時(shí)間:2022年09月30日 11:23:00   作者:融極  
這篇文章主要介紹了SpringBoot中的ApplicationRunner與CommandLineRunner問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

概述

開(kāi)發(fā)中可能會(huì)有這樣的場(chǎng)景,需要在容器啟動(dòng)的時(shí)候執(zhí)行一些內(nèi)容。比如讀取配置文件,數(shù)據(jù)庫(kù)連接之類的。

SpringBoot給我們提供了兩個(gè)接口來(lái)幫助我們實(shí)現(xiàn)這種需求。

兩個(gè)啟動(dòng)加載接口分別是:

  • CommandLineRunner
  • ApplicationRunner

他們的執(zhí)行時(shí)機(jī)是容器啟動(dòng)完成的時(shí)候。

實(shí)現(xiàn)啟動(dòng)加載接口

這兩個(gè)接口中有一個(gè)run方法,我們只需要實(shí)現(xiàn)這個(gè)方法即可。這個(gè)兩個(gè)接口的不同之處在于:

ApplicationRunner中的run方法的參數(shù)為ApplicationArguments,而CommandLineRunner接口中run方法的參數(shù)為String數(shù)組。

ApplicationRunner接口的示例

package com.jdddemo.demo.controller;
 
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
 
@Component
@Order(value = 1)
public class JDDRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(args);
        System.out.println("這個(gè)是測(cè)試ApplicationRunner接口");
    }
}

執(zhí)行結(jié)果如下:

CommandLineRunner接口示例

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
 
@Component
public class TestCommandLineRunner implements CommandLineRunner {
 
    @Override
    public void run(String... args) throws Exception {
        System.out.println("<這個(gè)是測(cè)試CommandLineRunn接口");
    }
}

CommandLineRunner和ApplicationRunner的執(zhí)行順序

在spring boot程序中,我們可以使用不止一個(gè)實(shí)現(xiàn)CommandLineRunner和ApplicationRunner的bean。

為了有序執(zhí)行這些bean的run()方法,可以使用@Order注解或Ordered接口。

下面例子中創(chuàng)建了兩個(gè)實(shí)現(xiàn)CommandLineRunner接口bean和兩個(gè)實(shí)現(xiàn)ApplicationRunner接口的bean。

我們使用@Order注解按順序執(zhí)行這四個(gè)bean

CommandLineRunnerBean1.java

@Component
@Order(1)
public class CommandLineRunnerBean1 implements CommandLineRunner {
? ? @Override
? ? public void run(String... args) {
? ? ? ? System.out.println("CommandLineRunnerBean 1");
? ? }
}

ApplicationRunnerBean1.java

@Component
@Order(2)
public class ApplicationRunnerBean1 implements ApplicationRunner {
? ? @Override
? ? public void run(ApplicationArguments arg0) throws Exception {
? ? ? ? System.out.println("ApplicationRunnerBean 1");
? ? }
}

CommandLineRunnerBean2.java

@Component
@Order(3)
public class CommandLineRunnerBean2 implements CommandLineRunner {
? ? @Override
? ? public void run(String... args) {
? ? ? ? System.out.println("CommandLineRunnerBean 2");
? ? }
}

ApplicationRunnerBean2.java

@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
? ? @Override
? ? public void run(ApplicationArguments arg0) throws Exception {
? ? ? ? System.out.println("ApplicationRunnerBean 2");
? ? }
}

輸出結(jié)果為:

CommandLineRunnerBean 1
ApplicationRunnerBean 1
CommandLineRunnerBean 2
ApplicationRunnerBean 2

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java泛型類與泛型方法的定義詳解

    Java泛型類與泛型方法的定義詳解

    這篇文章主要介紹了Java泛型類與泛型方法的定義,結(jié)合實(shí)例形式詳細(xì)分析了java泛型類與泛型方法定義、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-08-08
  • Spring?Mvc中CommonsMultipartFile的特性實(shí)例詳解

    Spring?Mvc中CommonsMultipartFile的特性實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Spring?Mvc中CommonsMultipartFile特性的相關(guān)資料,SpringMVC擁有強(qiáng)大的靈活性,非侵入性和可配置性,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • SpringBoot結(jié)合JWT實(shí)現(xiàn)用戶登錄、注冊(cè)、鑒權(quán)

    SpringBoot結(jié)合JWT實(shí)現(xiàn)用戶登錄、注冊(cè)、鑒權(quán)

    用戶登錄、注冊(cè)及鑒權(quán)是我們基本所有系統(tǒng)必備的,也是很核心重要的一塊,本文主要介紹了SpringBoot結(jié)合JWT實(shí)現(xiàn)用戶登錄、注冊(cè)、鑒權(quán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2023-05-05
  • Java中volatile關(guān)鍵字的作用

    Java中volatile關(guān)鍵字的作用

    這篇文章主要介紹了Java中volatile關(guān)鍵字的作用,文章基于Java的相關(guān)資料展開(kāi)對(duì)volatile關(guān)鍵字作用的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • java程序設(shè)計(jì)語(yǔ)言的優(yōu)勢(shì)及特點(diǎn)

    java程序設(shè)計(jì)語(yǔ)言的優(yōu)勢(shì)及特點(diǎn)

    在本篇文章里小編給大家分享的是一篇關(guān)于java程序設(shè)計(jì)語(yǔ)言的優(yōu)勢(shì)及特點(diǎn)的內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • SpringBoot 自定義注解異步記錄復(fù)雜日志詳解

    SpringBoot 自定義注解異步記錄復(fù)雜日志詳解

    這篇文章主要為大家介紹了SpringBoot 自定義注解異步記錄復(fù)雜日志詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 深入理解Spring Cache框架

    深入理解Spring Cache框架

    今天給大家分析一下 Spring 框架本身對(duì)這些緩存具體實(shí)現(xiàn)的支持和融合。使用 Spring Cache 將大大的減少我們的Spring項(xiàng)目中緩存使用的復(fù)雜度,提高代碼可讀性。本文將從以下幾個(gè)方面來(lái)認(rèn)識(shí)Spring Cache框架。感興趣的小伙伴們可以參考一下
    2018-11-11
  • MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問(wèn)題

    MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java中的Unsafe工具類使用詳解

    Java中的Unsafe工具類使用詳解

    這篇文章主要介紹了Java中的Unsafe工具類使用詳解,Unsafe是jdk提供的一個(gè)直接訪問(wèn)操作系統(tǒng)資源的工具類(底層c++實(shí)現(xiàn)),它可以直接分配內(nèi)存,內(nèi)存復(fù)制,copy,提供cpu級(jí)別的CAS樂(lè)觀鎖等操作,需要的朋友可以參考下
    2023-12-12
  • Spring內(nèi)存緩存Caffeine的基本使用教程分享

    Spring內(nèi)存緩存Caffeine的基本使用教程分享

    Caffeine作為當(dāng)下本地緩存的王者被大量的應(yīng)用再實(shí)際的項(xiàng)目中,可以有效的提高服務(wù)吞吐率、qps,降低rt,本文就來(lái)簡(jiǎn)單介紹下Caffeine的使用姿勢(shì)吧
    2023-03-03

最新評(píng)論