日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用CommandLineRunner或ApplicationRunner接口创建bean

發布時間:2025/3/17 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.concretepage</groupId> <artifactId>spring-boot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-demo</name> <description>Spring Boot Demo Project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

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; @Component public class CommandLineRunnerBean implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(CommandLineRunnerBean.class); public void run(String... args) { String strArgs = Arrays.stream(args).collect(Collectors.joining("|")); logger.info("Application started with arguments:" + strArgs); } }

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; @SpringBootApplication public class MyApplication { private static final Logger logger = LoggerFactory.getLogger(MyApplication.class); public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args); HelloService service = context.getBean(HelloService.class); logger.info(service.getMessage()); } }

我們也可以創建一個service。一旦Spring boot啟動完成service就會執行。這意味著SpringApplication.run()執行完成后service的方法就會執行。
HelloService.java

package com.concretepage.service;import org.springframework.stereotype.Service;@Service public class HelloService { public String getMessage(){ return "Hello World!"; } }

現在使用帶有參數的可執行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; @Component public class ApplicationRunnerBean implements ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerBean.class); @Override public void run(ApplicationArguments arg0) throws Exception { String strArgs = Arrays.stream(arg0.getSourceArgs()).collect(Collectors.joining("|")); logger.info("Application started with arguments:" + strArgs); } }

創建可執行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

@Component @Order(1) public class CommandLineRunnerBean1 implements CommandLineRunner { 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 { 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"); } }

輸出結果為:

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。