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

SpringBoot中的ApplicationRunner與CommandLineRunner問題

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

概述

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

SpringBoot給我們提供了兩個接口來幫助我們實現(xiàn)這種需求。

兩個啟動加載接口分別是:

  • CommandLineRunner
  • ApplicationRunner

他們的執(zhí)行時機是容器啟動完成的時候。

實現(xiàn)啟動加載接口

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

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("這個是測試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("<這個是測試CommandLineRunn接口");
    }
}

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

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

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

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

我們使用@Order注解按順序執(zhí)行這四個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

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

相關(guān)文章

最新評論

玉树县| 六枝特区| 大宁县| 桐庐县| 图片| 武汉市| 齐齐哈尔市| 舒兰市| 务川| 隆安县| 衡南县| 中西区| 康定县| 城步| 东兴市| 岳西县| 定安县| 祁门县| 临邑县| 雅江县| 玉门市| 大化| 临安市| 即墨市| 新乡市| 化隆| 太保市| 恭城| 阳原县| 深水埗区| 邳州市| 高邮市| 榆树市| 大庆市| 双江| 广平县| 民勤县| 个旧市| 堆龙德庆县| 太谷县| 麻阳|