spring boot 命令行啟動(dòng)的方式
在使用spring boot 構(gòu)建應(yīng)用啟動(dòng)時(shí),我們?cè)诠ぷ髦卸际峭ㄟ^(guò)命令行來(lái)啟動(dòng)應(yīng)用,有時(shí)候會(huì)需要一些特定的參數(shù)以在應(yīng)用啟動(dòng)時(shí),做一些初始化的操作。
spring boot 提供了 CommandLineRunner 和 ApplicationRunner 這兩個(gè)接口供用戶使用。
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 運(yùn)行結(jié)果
運(yùn)行: 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 會(huì)根據(jù) 是否匹配 --key=value 來(lái)解析參數(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 運(yùn)行結(jié)果
運(yùn)行命令 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 都能實(shí)現(xiàn)命令行應(yīng)用啟動(dòng)時(shí)根據(jù)參數(shù)獲取我們需要的值,做特殊的邏輯。但兩者有所不同,推薦使用 ApplicationRunner 的 optional 參數(shù), 方便擴(kuò)展。
4. 參考文檔
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis攔截器動(dòng)態(tài)替換表名的方法詳解
因?yàn)槲覀兂志脤涌蚣芨嗟厥褂肕yBatis,那我們就借助于MyBatis的攔截器來(lái)完成我們的功能,這篇文章主要給大家介紹了關(guān)于MyBatis攔截器動(dòng)態(tài)替換表名的相關(guān)資料,需要的朋友可以參考下2022-04-04使用SpringAOP實(shí)現(xiàn)公共字段填充功能
在新增員工或者新增菜品分類時(shí)需要設(shè)置創(chuàng)建時(shí)間、創(chuàng)建人、修改時(shí)間、修改人等字段,在編輯員工或者編輯菜品分類時(shí)需要設(shè)置修改時(shí)間、修改人等字段,這些字段屬于公共字段,本文將給大家介紹使用SpringAOP實(shí)現(xiàn)公共字段填充功能,需要的朋友可以參考下2024-08-08Spring Boot 與 Kotlin 上傳文件的示例代碼
這篇文章主要介紹了Spring Boot 與 Kotlin 上傳文件的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01基于SpringBoot創(chuàng)建Web頁(yè)面并熱更新的操作步驟
SpringBoot是一個(gè)用于快速開(kāi)發(fā)單個(gè)微服務(wù)的框架,它基于 Spring 框架,簡(jiǎn)化了Spring應(yīng)用的初始化過(guò)程和開(kāi)發(fā)流程,本文給大家介紹了如何基于SpringBoot創(chuàng)建Web頁(yè)面并熱更新,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11JAVA正則表達(dá)式提取key-value類型字符值代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于JAVA正則表達(dá)式提取key-value類型字符值的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-10-10Springboot集成Ehcache3實(shí)現(xiàn)本地緩存的配置方法
EhCache是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,是 Hibernate 中默認(rèn)的 CacheProvider,同Redis一樣,EhCache 不是純內(nèi)存緩存,它支持基于內(nèi)存和磁盤(pán)的二級(jí)緩存,本文介紹Springboot集成Ehcache3實(shí)現(xiàn)本地緩存的配置方法,感興趣的朋友一起看看吧2024-04-04java模板引擎Thymeleaf和前端vue的區(qū)別及說(shuō)明
這篇文章主要介紹了java模板引擎Thymeleaf和前端vue的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11