spring boot 命令行啟動的方式
在使用spring boot 構建應用啟動時,我們在工作中都是通過命令行來啟動應用,有時候會需要一些特定的參數以在應用啟動時,做一些初始化的操作。
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 運行結果
運行: java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -sdfsaf sdfas,
結果如下:
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 的使用,只是把參數根據空格分割。
- ApplicationRunner 會根據 是否匹配 --key=value 來解析參數,
- 能匹配,則為 optional 參數, 可用getOptionValues獲取參數值。
- 不匹配則是 non optional 參數。
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 運行結果
運行命令 java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -non1 non2 --option=1, 結果為:
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 參數名有 option, non optional 參數有 -non1 和 non2
3. 小結
CommandLineRunner 和 ApplicationRunner 都能實現命令行應用啟動時根據參數獲取我們需要的值,做特殊的邏輯。但兩者有所不同,推薦使用 ApplicationRunner 的 optional 參數, 方便擴展。
4. 參考文檔
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring Boot使用線程池創(chuàng)建多線程的完整示例
在 Spring Boot 2 中,可以使用 @Autowired 注入 線程池(ThreadPoolTaskExecutor 或 ExecutorService),從而管理線程的創(chuàng)建和執(zhí)行,以下是使用 @Autowired 方式注入線程池的完整示例,感興趣的朋友一起看看吧2025-03-03
java中InputStream轉為MultipartFile的解決方案
這篇文章主要介紹了java中InputStream轉為MultipartFile的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
SpringBoot 整合 RabbitMQ 的使用方式(代碼示例)
本文詳細介紹了使用RabbitTemplate進行消息傳遞的幾種模式,包括點對點通信、發(fā)布/訂閱模式、工作隊列模式、路由模式和主題模式,每種模式都通過代碼示例展示了生產者和消費者的實現,幫助開發(fā)者理解和運用RabbitMQ進行高效的消息處理2024-10-10
springboot @ConditionalOnMissingBean注解的作用詳解
這篇文章主要介紹了springboot @ConditionalOnMissingBean注解的作用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

