SpringBoot中的ApplicationRunner與CommandLineRunner問(wèn)題
概述
開(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)加載接口分別是:
CommandLineRunnerApplicationRunner
他們的執(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)文章
SpringBoot @PostConstruct和@PreDestroy的使用說(shuō)明
這篇文章主要介紹了SpringBoot @PostConstruct和@PreDestroy的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java中==運(yùn)算符與equals方法的區(qū)別及intern方法詳解
這篇文章主要介紹了Java中==運(yùn)算符與equals方法的區(qū)別及intern方法詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
通過(guò)實(shí)例了解java TransferQueue
這篇文章主要介紹了TransferQueue實(shí)例,下面小編和大家一起來(lái)學(xué)習(xí)一下2019-05-05
SpringBoot?如何通過(guò)?Profile?實(shí)現(xiàn)不同環(huán)境下的配置切換
SpringBoot通過(guò)profile實(shí)現(xiàn)在不同環(huán)境下的配置切換,比如常見(jiàn)的開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境,SpringBoot常用配置文件主要有?2?種:properties?文件和yml文件,本文給大家詳細(xì)介紹SpringBoot?通過(guò)?Profile?實(shí)現(xiàn)不同環(huán)境下的配置切換,感興趣的朋友一起看看吧2022-08-08
Java 日期格式加上指定月數(shù)(一個(gè)期限)得到一個(gè)新日期的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 日期格式加上指定月數(shù)(一個(gè)期限)得到一個(gè)新日期的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-05-05
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(22)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07

