2 开发Spring Shell应用程序

  向shell提供命令非常简单,需要学习的注解很少。该命令的实现风格与使用依赖注入的应用程序的开发类相同,您可以利用Spring容器的所有特性来实现您的命令类。

  spring-shell官网地址:​​https://projects.spring.io/spring-shell/​

2.1 标记接口

  创建命令的第一步是实现标记接口CommandMarker,并使用Spring的@Component注解对类进行注解(注意一个JIRA问题:提供@CliCommand元注解避免使用标记接口)。使用helloworld示例应用程序中的代码为例,HelloWorldCommands类的代码如下所示:



1 @Component
2 public class HelloWorldCommands implements CommandMarker {
3
4 // use any Spring annotations for Dependency Injection or other Spring interfaces
5 // as required.
6
7 // methods with @Cli annotations go here
8
9 }


 

2.2 日志

  目前,日志记录是使用JDK日志记录的。由于控制台、JLine和Ansi处理的复杂性,一般都建议将消息作为返回值的方式显示在shell窗口。但是,当需要进行日志记录时,典型的JDK logger声明就足够了。



1 @Component
2 public class HelloWorldCommands implements CommandMarker {
3
4 protected final Logger LOG = Logger.getLogger(getClass().getName());
5
6 // methods with @Cli annotations go here
7
8 }


  注意:一般开发人员的职责是为第三方库处理日志,应当要减少日志级别,这样控制台或者shell窗口就不会受到日志消息的影响。

 

2.3 CLI注解

  在方法和方法参数上使用了三个注释,这些注释定义了与shell交互的主要契约,分别是:

  • CliAvailabilityIndicator - 放在一个方法前返回一个布尔值,表示在shell中是否可以执行一个特定的命令。这个决定通常基于之前执行的命令的历史。它可以防止在满足某些先决条件时出现外部命令,例如执行'configuration'命令。
  • CliCommand - 放置在向shell提供命令的方法上。它的值提供了一个或多个字符串,这些字符串作为特定命令名的开始。在整个应用程序中,包括所有的插件中这些必须是唯一的。
  • CliOption - 放置在命令方法的参数中,允许它默认值声明参数值为必填的或可选的。

  下面是在命令类中使用这些注解的简单用法



1 @Component
2 public class HelloWorldCommands implements CommandMarker {
3
4 @CliAvailabilityIndicator({"hw simple"})
5 public boolean isCommandAvailable() {
6 return true;
7 }
8
9 @CliCommand(value = "hw simple", help = "Print a simple hello world message")
10 public String simple(
11 @CliOption(key = { "message" }, mandatory = true, help = "The hello world message")
12 final String message,
13
14 @CliOption(key = { "location" }, mandatory = false,
15 help = "Where you are saying hello", specifiedDefaultValue="At work")
16 final String location) {
17
18 return "Message = [" + message + "] Location = [" + location + "]";
19
20 }
21 }


 

  注解@CliAvailabilityIndicator方法返回true,这是这个类中暴露给shell调用的唯一的命令。如果类中有更多的命令,则将它们作为逗号分隔值列出。

  @CliCommand注解是创建shell命令'hw simple'。帮助消息是如果您使用帮助命令'help'将会打印什么内容。这里定义方法名是“simple”,但它可以是任何自定义的名称。

  @CliOption注解在每个命令的参数。您需要决定哪些参数是必需的,哪些是可选的,如果它们是可选的,则有一个默认值。在本例中,该命令有两个参数:消息'message'和位置'location'。需要使用消息选项,并提供一个帮助消息,以便在为该命令完成任务时为用户提供指导。

  “simple”方法的实现很简单,只是一个日志语句,但这是通常调用的其他对象,这些对象是通过Spring注入到类中的,然后可以实现复杂的功能。

  本例中的方法参数类型是String,它不会出现类型转换的任何问题。您可以指定任何的对象类型以及基本数据类型,如int, float等。对所有类型以外由默认shell(基本数据类型, Date, File)需要在您的插件中与容器的转换器接口org.springframework.shell.core.Converter 注册它的转换。

  注意,方法返回参数可以是非void,在我们的示例中,它是我们想要显示的实际消息。返回非void类型时,shell将显示为它的toString()字符。

2.4 测试shell命令

  执行测试的shell命令,您可以实例化shell在一个测试用例中执行命令,然后在返回值CommandResult执行断言。一个简单的基类设置如下所示:



1 public abstract class AbstractShellIntegrationTest {
2
3 private static JLineShellComponent shell;
4
5 @BeforeClass
6 public static void startUp() throws InterruptedException {
7 Bootstrap bootstrap = new Bootstrap();
8 shell = bootstrap.getJLineShellComponent();
9 }
10
11 @AfterClass
12 public static void shutdown() {
13 shell.stop();
14 }
15
16 public static JLineShellComponent getShell() {
17 return shell;
18 }
19
20 }


 

  这里有一个测试日期命令的例子:



1 public class BuiltInCommandTests extends AbstractShellIntegrationTest {
2
3 @Test
4 public void dateTest() throws ParseException {
5
6 //Execute command
7 CommandResult cr = getShell().executeCommand("date");
8
9 //Get result
10 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US);
11 Date result = df.parse(cr.getResult().toString());
12
13 //Make assertions - DateMaters is an external dependency not shown here.
14 Date now = new Date();
15 MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result));
16 }
17 }


 

   CommandResult的getResult方法返回的 java.lang.Class将匹配与@CliCommand注解方法的返回值。您应该向适当的类型转换,以帮助执行您的断言。

 

2.5 构建和运行shell

  在我们看来,构建和执行shell最简单的方法是剪切和粘贴脚本。这将使来自于分级的应用程序插件创建一个bin目录,该目录带有用于windows和Unix的启动脚本,并将所有依赖jar放在lib目录中。Maven有一个类似的插件——AppAssembler插件。

  shell的主类是org.springframework.shell.Bootstrap的。只要您在类路径上放置其他的插件,或者是独立开发的,引导类就会将它们合并到shell中。

 

 


把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~