Spring Shell
看到Spring官网中有一个spring shell今天又空就去了解了一下Spring shell的内容。
Spring Shell 用途:有时并非所有的应用程序都需要一个花哨的Web用户界面,使用交互式终端与应用程序进行交互是完成任务的最合适的方式。
Spring Shell允许用户轻松创建这样一个可运行的应用程序,用户将输入文本命令,直到程序终止时才会执行。 Spring Shell项目提供了创建这种REPL(读取,评估,打印循环)的基础结构,使开发人员可以使用熟悉的Spring编程模型专注于命令的实现。
诸如解析,TAB完成,输出着色,花式ASCII艺术表格显示,输入转换和验证等高级功能都是免费的,开发人员只需要关注核心命令逻辑。
总结一下就是:Spring shell 可以实现一个简单的命令交互,你使用这个只需要专注于逻辑就可以了。
代码:
@ShellComponent
public class AddShell {
@ShellMethod("Add two integers together.")
public int add(int a, int b) {
return a + b;
}
}
maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
运行Spring boot项目后效果如下:
shell:>help
AVAILABLE COMMANDS
Add Shell
add: Add two integers together.
Built-In Commands
clear: Clear the shell screen.
exit, quit: Exit the shell.
help: Display help about available commands.
script: Read and execute commands from a file.
stacktrace: Display the full stacktrace of the last error.
shell:>clear
shell:>