spring boot 命令行啟動的方式
在使用spring boot 構(gòu)建應(yīng)用啟動時,我們在工作中都是通過命令行來啟動應(yīng)用,有時候會需要一些特定的參數(shù)以在應(yīng)用啟動時,做一些初始化的操作。
spring boot 提供了 CommandLineRunner 和 ApplicationRunner 這兩個接口供用戶使用。
1. CommandLineRunner
1.1 聲明:
@FunctionalInterface public interface CommandLineRunner { /** * Callback used to run the bean. * @param args incoming main method arguments * @throws Exception on error */ void run(String... args) throws Exception; }
1.2 使用:
package com.example.consoleapplication; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class TestRunner implements CommandLineRunner { @Override public void run(String... args) { // Do something... for(String arg: args){ System.out.println(arg); } System.out.print("test command runner"); } }
1.3 運行結(jié)果
運行: java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -sdfsaf sdfas,
結(jié)果如下:
2019-03-16 17:31:56.544 INFO 18679 --- [ main] c.e.consoleapplication.DemoApplication : No active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195 INFO 18679 --- [ main] c.e.consoleapplication.DemoApplication : Started DemoApplication in 16.172 seconds (JVM running for 16.65)
-sdfsaf
sdfas
test command runner%
2. ApplicationRunner
2.1 聲明
/** * Interface used to indicate that a bean should <em>run</em> when it is contained within * a {@link SpringApplication}. Multiple {@link ApplicationRunner} beans can be defined * within the same application context and can be ordered using the {@link Ordered} * interface or {@link Order @Order} annotation. * * @author Phillip Webb * @since 1.3.0 * @see CommandLineRunner */ @FunctionalInterface public interface ApplicationRunner { /** * Callback used to run the bean. * @param args incoming application arguments * @throws Exception on error */ void run(ApplicationArguments args) throws Exception; }
2.2 使用
ApplicationRunner 和 CommandLineRunner 的使用是有差別的:
- CommandLineRunner 的使用,只是把參數(shù)根據(jù)空格分割。
- ApplicationRunner 會根據(jù) 是否匹配 --key=value 來解析參數(shù),
- 能匹配,則為 optional 參數(shù), 可用getOptionValues獲取參數(shù)值。
- 不匹配則是 non optional 參數(shù)。
package com.example.consoleapplication; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import org.springframework.boot.ApplicationArguments; @Component public class TestApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // Do something... System.out.println("option arg names" + args.getOptionNames()); System.out.println("non option+" + args.getNonOptionArgs()); } }
2.3 運行結(jié)果
運行命令 java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -non1 non2 --option=1
, 結(jié)果為:
2019-03-16 18:08:08.528 INFO 19778 --- [ main] c.e.consoleapplication.DemoApplication : No active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166 INFO 19778 --- [ main] c.e.consoleapplication.DemoApplication : Started DemoApplication in 16.059 seconds (JVM running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
--option=1
test%
可以看到, optional 參數(shù)名有 option, non optional 參數(shù)有 -non1 和 non2
3. 小結(jié)
CommandLineRunner 和 ApplicationRunner 都能實現(xiàn)命令行應(yīng)用啟動時根據(jù)參數(shù)獲取我們需要的值,做特殊的邏輯。但兩者有所不同,推薦使用 ApplicationRunner 的 optional 參數(shù), 方便擴(kuò)展。
4. 參考文檔
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 與 Kotlin 上傳文件的示例代碼
這篇文章主要介紹了Spring Boot 與 Kotlin 上傳文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01基于SpringBoot創(chuàng)建Web頁面并熱更新的操作步驟
SpringBoot是一個用于快速開發(fā)單個微服務(wù)的框架,它基于 Spring 框架,簡化了Spring應(yīng)用的初始化過程和開發(fā)流程,本文給大家介紹了如何基于SpringBoot創(chuàng)建Web頁面并熱更新,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11JAVA正則表達(dá)式提取key-value類型字符值代碼實例
這篇文章主要給大家介紹了關(guān)于JAVA正則表達(dá)式提取key-value類型字符值的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-10-10Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法
EhCache是一個純Java的進(jìn)程內(nèi)緩存框架,是 Hibernate 中默認(rèn)的 CacheProvider,同Redis一樣,EhCache 不是純內(nèi)存緩存,它支持基于內(nèi)存和磁盤的二級緩存,本文介紹Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法,感興趣的朋友一起看看吧2024-04-04java模板引擎Thymeleaf和前端vue的區(qū)別及說明
這篇文章主要介紹了java模板引擎Thymeleaf和前端vue的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11