使用CommandLineRunner或ApplicationRunner接口创建bean
在spring boot應(yīng)用中,我們可以在程序啟動(dòng)之前執(zhí)行任何任務(wù)。為了達(dá)到這個(gè)目的,我們需要使用CommandLineRunner或ApplicationRunner接口創(chuàng)建bean,spring boot會(huì)自動(dòng)監(jiān)測(cè)到它們。這兩個(gè)接口都有一個(gè)run()方法,在實(shí)現(xiàn)接口時(shí)需要覆蓋該方法,并使用@Component注解使其成為bean。CommandLineRunner和ApplicationRunner的作用是相同的。不同之處在于CommandLineRunner接口的run()方法接收String數(shù)組作為參數(shù),而ApplicationRunner接口的run()方法接收ApplicationArguments對(duì)象作為參數(shù)。當(dāng)程序啟動(dòng)時(shí),我們傳給main()方法的參數(shù)可以被實(shí)現(xiàn)CommandLineRunner和ApplicationRunner接口的類的run()方法訪問。我們可以創(chuàng)建多個(gè)實(shí)現(xiàn)CommandLineRunner和ApplicationRunner接口的類。為了使他們按一定順序執(zhí)行,可以使用@Order注解或?qū)崿F(xiàn)Ordered接口。
CommandLineRunner和ApplicationRunner接口的run()方法在SpringApplication完成啟動(dòng)時(shí)執(zhí)行。啟動(dòng)完成之后,應(yīng)用開始運(yùn)行。CommandLineRunner和ApplicationRunner的作用是在程序開始運(yùn)行前執(zhí)行任務(wù)或記錄信息。
下面展示了如何在程序中使用CommandLineRunner和ApplicationRunner。
Maven配置
pom.xml
CommandLineRunner
CommandLineRunner是個(gè)接口,有一個(gè)run()方法。為了使用CommandLineRunner我們需要?jiǎng)?chuàng)建一個(gè)類實(shí)現(xiàn)該接口并覆蓋run()方法。使用@Component注解實(shí)現(xiàn)類。當(dāng)SpringApplication.run()啟動(dòng)spring boot程序時(shí),啟動(dòng)完成之前,CommandLineRunner.run()會(huì)被執(zhí)行。CommandLineRunner的run()方法接收啟動(dòng)服務(wù)時(shí)傳過(guò)來(lái)的參數(shù)。
run(String... args)參數(shù)為String數(shù)組。
CommandLineRunnerBean.java
package com.concretepage.bean;import java.util.Arrays; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;MyApplication.java
package com.concretepage;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.concretepage.service.HelloService;我們也可以創(chuàng)建一個(gè)service。一旦Spring boot啟動(dòng)完成service就會(huì)執(zhí)行。這意味著SpringApplication.run()執(zhí)行完成后service的方法就會(huì)執(zhí)行。
HelloService.java
現(xiàn)在使用帶有參數(shù)的可執(zhí)行jar運(yùn)行程序。spring-boot-demo-0.0.1-SNAPSHOT.jar為生成的jar文件。執(zhí)行命令如下:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3輸出結(jié)果為:
2017-03-19 13:38:38.909 INFO 1036 --- [ main] c.c.bean.CommandLineRunnerBean : Application started with arguments:data1|data2|data3 2017-03-19 13:38:38.914 INFO 1036 --- [ main] com.concretepage.MyApplication : Started MyApplication in 1.398 seconds (JVM running for 1.82) 2017-03-19 13:38:38.915 INFO 1036 --- [ main] com.concretepage.MyApplication : Hello World!ApplicationRunner
ApplicationRunner和CommandLineRunner的作用相同。在SpringApplication.run()完成spring boot啟動(dòng)之前,ApplicationRunner的run()方法會(huì)被執(zhí)行。
run(ApplicationArguments args)可以看到,CommandLineRunner.run()接收String數(shù)組作為參數(shù),而ApplicationRunner.run()接收ApplicationArguments作為參數(shù)。這些參數(shù)是啟動(dòng)spring boot程序時(shí)傳給main()方法的。
ApplicationRunnerBean.java
package com.concretepage.bean;import java.util.Arrays; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component;創(chuàng)建可執(zhí)行jar包并使用如下參數(shù)運(yùn)行:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3輸出結(jié)果為:
2017-03-19 16:26:06.952 INFO 5004 --- [ main] c.c.bean.ApplicationRunnerBean : Application started with arguments:data1|data2|data3 2017-03-19 16:26:06.956 INFO 5004 --- [ main] com.concretepage.MyApplication : Started MyApplication in 1.334 seconds (JVM running for 1.797) 2017-03-19 16:26:06.957 INFO 5004 --- [ main] com.concretepage.MyApplication : Hello World!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
ApplicationRunnerBean1.java
CommandLineRunnerBean2.java
ApplicationRunnerBean2.java
輸出結(jié)果為:
CommandLineRunnerBean 1 ApplicationRunnerBean 1 CommandLineRunnerBean 2 ApplicationRunnerBean 2作者:Java_Explorer
鏈接:https://www.jianshu.com/p/de7b0e124248
來(lái)源:簡(jiǎn)書
簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。
轉(zhuǎn)載于:https://www.cnblogs.com/maohuidong/p/10032810.html
總結(jié)
以上是生活随笔為你收集整理的使用CommandLineRunner或ApplicationRunner接口创建bean的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [数学笔记Mathematical No
- 下一篇: Eclipse启动之一:外壳程序(百度空