在使用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 的使用是有差别的:
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. 参考文档
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-web-environment
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。