官方对于SpringShell的介绍总结起来,差不多就是需要输入控制,不需要花里胡哨的Web相关的东西。只要一个控制台相关的。进行测试即可。
首先在项目中包含最小的SpringBoot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
其次是包含SpringShell的包
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
下面介绍几个注解:
@ShellComponent:为@Component的变体,注解到类上。
@ShellMethod:注解到方法上,里面可以加一些描述。
如下示例代码:
博主小例子
关键源码如下:
@ShellComponent
public class MyShellTest {
@ShellMethod("Add")
public Integer add(Integer a, Integer b){
return a + b;
}
@ShellMethod("print")
public String print(String name, Integer id){
String ret = "The name is " + name + ", The id is " + id;
return ret;
}
}
这里面有个BUG要说明下这里的Shell方法需要全小写,当有个字母大写,则调用会有问题。
程序运行截图如下:
源码打包下载地址: