小編給大家分享一下CommandLineRunner與ApplicationRunner是什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
專業(yè)從事網(wǎng)站制作、成都網(wǎng)站制作,高端網(wǎng)站制作設(shè)計(jì),重慶小程序開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊(duì)竭力真誠服務(wù),采用H5建站+CSS3前端渲染技術(shù),響應(yīng)式網(wǎng)站,讓網(wǎng)站在手機(jī)、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項(xiàng)小組,與您實(shí)時(shí)在線互動(dòng),隨時(shí)提供解決方案,暢聊想法和感受。
CommandLineRunner和ApplicationRunner是Spring Boot所提供的接口,他們都有一個(gè)run()方法。所有實(shí)現(xiàn)他們的Bean都會(huì)在Spring Boot服務(wù)啟動(dòng)之后自動(dòng)地被調(diào)用。
由于這個(gè)特性,它們是一個(gè)理想地方去做一些初始化的工作,或者寫一些測試代碼。
CommandLineRunner
使用Application實(shí)現(xiàn)
在我們新建好工程后,為了簡單我們直接使用Application類實(shí)現(xiàn)CommandLineRunner接口,這個(gè)類的注解@SpringBootApplication會(huì)為我們自動(dòng)配置。
package cn.examplecode.sb2runner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Sb2runnerApplication implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(Sb2runnerApplication.class); public static void main(String[] args) { SpringApplication.run(Sb2runnerApplication.class, args); } @Override public void run(String... args) throws Exception { logger.info("服務(wù)已啟動(dòng),執(zhí)行command line runner。"); for (int i = 0; i < args.length; ++i) { logger.info("args[{}]: {}", i, args[i]); } } }
接下來我們直接啟動(dòng)服務(wù),查看日志如下,發(fā)現(xiàn)run()方法被正常地執(zhí)行了:
Tomcat started on port(s): 8080 (http) with context path '' Started Sb2runnerApplication in 2.204 seconds (JVM running for 3.161) 服務(wù)已啟動(dòng),執(zhí)行command line runner。
run()方法有個(gè)可變參數(shù)args,這個(gè)參數(shù)是用來接收命令行參數(shù)的,我們下面來加入?yún)?shù)來測試一下:
然后重啟服務(wù),觀察日志,可以看到參數(shù)被正常地接收到了:
Tomcat started on port(s): 8080 (http) with context path '' Started Sb2runnerApplication in 1.888 seconds (JVM running for 2.41) 服務(wù)已啟動(dòng),執(zhí)行command line runner。 args[0]: --param=sth
命令行參數(shù)傳遞
之前我們說過使用Spring Boot的一大優(yōu)勢就是可以將工程直接打包成一個(gè)jar包而不需要單獨(dú)部署。打包成jar包后可以直接執(zhí)行該jar包進(jìn)行服務(wù)的啟動(dòng),這樣在執(zhí)行jar包時(shí)我們就可以傳入命令行參數(shù),讓CommandLineRunner接收參數(shù)。
這種場景在服務(wù)器上特別常用。比如我們想執(zhí)行某個(gè)操作,又不想對外部暴露,此時(shí)可以使用CommandLineRunner作為該操作的入口。
下面我們就打成jar包來演示一下。
可以從日志中看到我們也正常地獲取到了參數(shù)。通過傳遞參數(shù),在業(yè)務(wù)邏輯上我們可以根據(jù)不同的參數(shù)而執(zhí)行不同的操作。
上面我們提到的只是一個(gè)CommandLineRunner,如果我們有多個(gè)CommandLineRunner怎么辦呢?怎么控制它們執(zhí)行的順序呢?
下面我們就來介紹如何指定執(zhí)行的順序。
Spring Boot為我們提供了一個(gè)注解"@Order",可以用來指定執(zhí)行的順序,比如我們工程里面有三個(gè)CommandLineRunner:
@Component @Order(1) public class CommandRunner1 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CommandRunner1.class); @Override public void run(String... args) throws Exception { logger.info("執(zhí)行第一個(gè)command line runner..."); } } @Component @Order(2) public class CommandRunner2 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CommandRunner2.class); @Override public void run(String... args) throws Exception { logger.info("執(zhí)行第二個(gè)command line runner..."); } } @Component @Order(3) public class CommandRunner3 implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(CommandRunner3.class); @Override public void run(String... args) throws Exception { logger.info("執(zhí)行第三個(gè)command line runner..."); } }
我們可以在該類的上面直接加入@Order注解,然后Spring Boot就會(huì)按照我們注解指定的順序從小到大的執(zhí)行了。很簡單,是不是?
Tomcat started on port(s): 8080 (http) with context path '' Started Sb2runnerApplication in 1.764 seconds (JVM running for 2.292) 執(zhí)行第一個(gè)command line runner... 執(zhí)行第二個(gè)command line runner... 執(zhí)行第三個(gè)command line runner...
ApplicationRunner與CommandLineRunner做的事情是一樣的,也是在服務(wù)啟動(dòng)之后其run()方法會(huì)被自動(dòng)地調(diào)用,唯一不同的是ApplicationRunner會(huì)封裝命令行參數(shù),可以很方便地獲取到命令行參數(shù)和參數(shù)值。
@Component public class ApplicationRunner1 implements ApplicationRunner { private static Logger logger = LoggerFactory.getLogger(ApplicationRunner1.class); @Override public void run(ApplicationArguments args) throws Exception { logger.info("執(zhí)行application runner..."); logger.info("獲取到參數(shù): " + args.getOptionValues("param")); } }
執(zhí)行結(jié)果:
我們可以發(fā)現(xiàn),通過run()方法的參數(shù)ApplicationArguments可以很方便地獲取到命令行參數(shù)的值。
所以如果你的工程需要獲取命令行參數(shù)的話,建議你使用ApplicationRunner。
總結(jié)
無論是CommandLineRunner還是ApplicationRunner,它們的目的都是在服務(wù)啟動(dòng)之后執(zhí)行一些操作。如果需要獲取命令行參數(shù)時(shí)則建議使用ApplicationRunner。
另一種場景是我們在服務(wù)器上需要執(zhí)行某個(gè)操作,比如修正數(shù)據(jù)庫用戶的數(shù)據(jù),而又找不到合適的執(zhí)行入口,那么這就是它們理想的使用場景了。
以上是CommandLineRunner與ApplicationRunner是什么的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:CommandLineRunner與ApplicationRunner是什么
文章源于:http://chinadenli.net/article46/ppgpeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、微信公眾號、做網(wǎng)站、小程序開發(fā)、云服務(wù)器、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)