您的位置:首页技术文章
文章详情页

Spring Shell应用程序开发流程解析

【字号: 日期:2023-08-06 17:54:43浏览:4作者:猪猪

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

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

标记接口

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

@Componentpublic class HelloWorldCommands implements CommandMarker { // use any Spring annotations for Dependency Injection or other Spring interfaces // as required. // methods with @Cli annotations go here}

日志

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

@Componentpublic class HelloWorldCommands implements CommandMarker { protected final Logger LOG = Logger.getLogger(getClass().getName()); // methods with @Cli annotations go here}

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

CLI注解

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

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

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

@Componentpublic class HelloWorldCommands implements CommandMarker { @CliAvailabilityIndicator({'hw simple'}) public boolean isCommandAvailable() { return true; } @CliCommand(value = 'hw simple', help = 'Print a simple hello world message') public String simple( @CliOption(key = { 'message' }, mandatory = true, help = 'The hello world message') final String message, @CliOption(key = { 'location' }, mandatory = false,help = 'Where you are saying hello', specifiedDefaultValue='At work') final String location) { return 'Message = [' + message + '] Location = [' + location + ']'; }}

注解@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()字符。

测试shell命令

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

public abstract class AbstractShellIntegrationTest { private static JLineShellComponent shell; @BeforeClass public static void startUp() throws InterruptedException { Bootstrap bootstrap = new Bootstrap(); shell = bootstrap.getJLineShellComponent(); } @AfterClass public static void shutdown() { shell.stop(); } public static JLineShellComponent getShell() { return shell; }}

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

public class BuiltInCommandTests extends AbstractShellIntegrationTest { @Test public void dateTest() throws ParseException { //Execute command CommandResult cr = getShell().executeCommand('date'); //Get result DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US); Date result = df.parse(cr.getResult().toString()); //Make assertions - DateMaters is an external dependency not shown here. Date now = new Date(); MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result)); }}

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

构建和运行shell

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Spring
相关文章: