使用CommandLineRunner或ApplicationRunner接口创建bean
在spring boot應用中,我們可以在程序啟動之前執行任何任務。為了達到這個目的,我們需要使用CommandLineRunner或ApplicationRunner接口創建bean,spring boot會自動監測到它們。這兩個接口都有一個run()方法,在實現接口時需要覆蓋該方法,并使用@Component注解使其成為bean。CommandLineRunner和ApplicationRunner的作用是相同的。不同之處在于CommandLineRunner接口的run()方法接收String數組作為參數,而ApplicationRunner接口的run()方法接收ApplicationArguments對象作為參數。當程序啟動時,我們傳給main()方法的參數可以被實現CommandLineRunner和ApplicationRunner接口的類的run()方法訪問。我們可以創建多個實現CommandLineRunner和ApplicationRunner接口的類。為了使他們按一定順序執行,可以使用@Order注解或實現Ordered接口。
CommandLineRunner和ApplicationRunner接口的run()方法在SpringApplication完成啟動時執行。啟動完成之后,應用開始運行。CommandLineRunner和ApplicationRunner的作用是在程序開始運行前執行任務或記錄信息。
下面展示了如何在程序中使用CommandLineRunner和ApplicationRunner。
Maven配置
pom.xml
CommandLineRunner
CommandLineRunner是個接口,有一個run()方法。為了使用CommandLineRunner我們需要創建一個類實現該接口并覆蓋run()方法。使用@Component注解實現類。當SpringApplication.run()啟動spring boot程序時,啟動完成之前,CommandLineRunner.run()會被執行。CommandLineRunner的run()方法接收啟動服務時傳過來的參數。
run(String... args)參數為String數組。
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;我們也可以創建一個service。一旦Spring boot啟動完成service就會執行。這意味著SpringApplication.run()執行完成后service的方法就會執行。
HelloService.java
現在使用帶有參數的可執行jar運行程序。spring-boot-demo-0.0.1-SNAPSHOT.jar為生成的jar文件。執行命令如下:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3輸出結果為:
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啟動之前,ApplicationRunner的run()方法會被執行。
run(ApplicationArguments args)可以看到,CommandLineRunner.run()接收String數組作為參數,而ApplicationRunner.run()接收ApplicationArguments作為參數。這些參數是啟動spring boot程序時傳給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;創建可執行jar包并使用如下參數運行:
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar data1 data2 data3輸出結果為:
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的執行順序
在spring boot程序中,我們可以使用不止一個實現CommandLineRunner和ApplicationRunner的bean。為了有序執行這些bean的run()方法,可以使用@Order注解或Ordered接口。例子中我們創建了兩個實現CommandLineRunner接口的bean和兩個實現ApplicationRunner接口的bean。我們使用@Order注解按順序執行這四個bean。
CommandLineRunnerBean1.java
ApplicationRunnerBean1.java
CommandLineRunnerBean2.java
ApplicationRunnerBean2.java
輸出結果為:
CommandLineRunnerBean 1 ApplicationRunnerBean 1 CommandLineRunnerBean 2 ApplicationRunnerBean 2作者:Java_Explorer
鏈接:https://www.jianshu.com/p/de7b0e124248
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。
轉載于:https://www.cnblogs.com/maohuidong/p/10032810.html
總結
以上是生活随笔為你收集整理的使用CommandLineRunner或ApplicationRunner接口创建bean的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [数学笔记Mathematical No
- 下一篇: Eclipse启动之一:外壳程序(百度空