spring boot 命令行启动的方式
在使用springboot构建应用启动时,我们在工作中都是通过命令行来启动应用,有时候会需要一些特定的参数以在应用启动时,做一些初始化的操作。
springboot提供了CommandLineRunner和ApplicationRunner这两个接口供用户使用。
1.CommandLineRunner
1.1声明:
@FunctionalInterface
publicinterfaceCommandLineRunner{
/**
*Callbackusedtorunthebean.
*@paramargsincomingmainmethodarguments
*@throwsExceptiononerror
*/
voidrun(String...args)throwsException;
}
1.2使用:
packagecom.example.consoleapplication;
importorg.springframework.boot.CommandLineRunner;
importorg.springframework.stereotype.Component;
@Component
publicclassTestRunnerimplementsCommandLineRunner{
@Override
publicvoidrun(String...args){
//Dosomething...
for(Stringarg:args){
System.out.println(arg);
}
System.out.print("testcommandrunner");
}
}
1.3运行结果
运行:java-jarbuild/libs/consoleapplication-0.0.1-SNAPSHOT.jar-sdfsafsdfas,
结果如下:
2019-03-1617:31:56.544 INFO18679---[ main]c.e.consoleapplication.DemoApplication :Noactiveprofileset,fallingbacktodefaultprofiles:default
2019-03-1617:31:57.195 INFO18679---[ main]c.e.consoleapplication.DemoApplication :StartedDemoApplicationin16.172seconds(JVMrunningfor16.65)
-sdfsaf
sdfas
testcommandrunner%
2.ApplicationRunner
2.1声明
/**
*Interfaceusedtoindicatethatabeanshouldrunwhenitiscontainedwithin
*a{@linkSpringApplication}.Multiple{@linkApplicationRunner}beanscanbedefined
*withinthesameapplicationcontextandcanbeorderedusingthe{@linkOrdered}
*interfaceor{@linkOrder@Order}annotation.
*
*@authorPhillipWebb
*@since1.3.0
*@seeCommandLineRunner
*/
@FunctionalInterface
publicinterfaceApplicationRunner{
/**
*Callbackusedtorunthebean.
*@paramargsincomingapplicationarguments
*@throwsExceptiononerror
*/
voidrun(ApplicationArgumentsargs)throwsException;
}
2.2使用
ApplicationRunner和CommandLineRunner的使用是有差别的:
- CommandLineRunner的使用,只是把参数根据空格分割。
- ApplicationRunner会根据是否匹配--key=value来解析参数,
- 能匹配,则为optional参数,可用getOptionValues获取参数值。
- 不匹配则是nonoptional参数。
packagecom.example.consoleapplication;
importorg.springframework.boot.ApplicationRunner;
importorg.springframework.stereotype.Component;
importorg.springframework.boot.ApplicationArguments;
@Component
publicclassTestApplicationRunnerimplementsApplicationRunner{
@Override
publicvoidrun(ApplicationArgumentsargs)throwsException{
//Dosomething...
System.out.println("optionargnames"+args.getOptionNames());
System.out.println("nonoption+"+args.getNonOptionArgs());
}
}
2.3运行结果
运行命令java-jarbuild/libs/consoleapplication-0.0.1-SNAPSHOT.jar-non1non2--option=1,结果为:
2019-03-1618:08:08.528 INFO19778---[ main]c.e.consoleapplication.DemoApplication :Noactiveprofileset,fallingbacktodefaultprofiles:default
2019-03-1618:08:09.166 INFO19778---[ main]c.e.consoleapplication.DemoApplication :StartedDemoApplicationin16.059seconds(JVMrunningfor16.56)
test
optionargnames[option]
nonoption+[-non1,non2]-non1
non2
--option=1
test%
可以看到,optional参数名有option,nonoptional参数有-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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。