• Spring Shell参考文档


      Spring Shell的核心组件是它的插件模型(plugin model)、内置命令(built-in commands)和转换器( converters)。


    1.1 Plugin Model(插件模型)


      插件模型是基于Spring的。每个插件jar需要包含的文件META-INF/spring/spring-shell-plugin.xml。当shell启动时,将加载这些配置文件以引导一个Spring ApplicationContext。其实现如下:

    new ClassPathXmlApplicationContext("classpath*:/META-INF/spring/spring-shell-plugin.xml");

      spring-shell-plugin.xml文件里定义了命令类和支持该命令操作的任何其他协作对象。下面的图中描述了插件模型:

     

      注意:当前核心命令解析器会加载下面所有的插件,建议提供一个类加载器进行隔离。

      1.1.1 Commands(命令)


      声明这些命令的一种简单方法是使用Spring的组件扫描功能。这是一个来自示例程序的例子spring-shell-plugin.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
      <context:component-scan 
               base-package="org.springframework.shell.samples.helloworld.commands" />
    
    </beans>

     

      这些命令是Spring组件,使用@Component注解进行划分。例如,HelloWorldCommands类的示例应用程序是这样的

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

     

      一旦这些命令注册并被Spring容器实例化,它们就会被注册到核心命令解析器中,这样就可以处理@cli注解了。通过实现CommandMarker接口,可以识别命令。

      1.1.2 Converters(转换器)

      org.springframework.shell.core.Converter接口提供了在命令行中输入的字符串转换成丰富的Java类型,作为@Cli方法的参数。

      通用类型转换程序是默认注册的,这些覆盖原始类型(boolean, int, float...)以及 Date、Character和File。

      如果你需要注册任何额外的转换器实例,可以在spring-shell-plugin.xml配置,Spring容它们将被识别转换。

    1.2 Built in commands(内置命令)
      允许执行的操作系统(OS)的命令
      以下是Spring shell提供的内置命令,按照类名-命令-功能的格式排列

    • ConsoleCommands - clr 和 clear - 清空控制台
    • DateCommands - date - 显示当前日期
    • ExitCommands - exit 和 quit - 退出shell
    • HelpCommands - help - 列出所有命令和它们的用法
    • InlineCommentCommands - // 和 ; - 内联注释标记
    • OsCommands - ! -允许执行的操作系统(OS)的命令。这个命令的关键字是感叹号,使用方法是在感叹号之后加一个空格,一个unix/windows命令字符串。
    • SystemPropertyCommands - system properties - 显示shell的系统属性
    • VersionCommands - version - 显示shell的版本

      有两个命令提供的AbstractShell类产品使用相关的注释块

    • / *和* /  注释块的开始和结束字符

    1.3 Customizing the shell(自定义shell)

      提供一些扩展点允许定制shell。扩展点是以下接口:

    • BannerProvider——指定标题文本,欢迎信息,版本号将在shell启动时显示
    • PromptProvider——指定命令提示文本,如:"shell>" 或 "#" 或 "$"”。这将在每次命令执行之后被调用,因此它需要是一个静态字符串。
    • HistoryFileNameProvider——指定命令历史文件的名称

      这些接口有一个默认的实现,但是可以为自己的shell应用程序创建自己的实现,即重写这些方法。所有这些接口从NamedProvider延伸。使用Spring的@Order注解来设置优先级。这允许您的实现优先于其他插件上的其他实现。


    1.4 插件之间的交互
      由于这是一个标准的Spring应用程序,您可以使用Spring的ApplicationContext事件基础设施来跨插件进行通信。

     

    1.5 命令方法拦截
      在调用命令方法时提供了一种简单的拦截方式。这使得命令类可以检查状态的更新,例如在执行命令方法之前,由其他插件修改的配置信息。使用此功能应该实现接口ExecutionProcessor代替CommandMarker。ExecutionProcessor接口如下所示:

     1 public interface ExecutionProcessor extends CommandMarker {
     2 
     3  /**
     4   * Method called before invoking the target command (described by {@link ParseResult}).
     5   * Additionally, for advanced cases, the parse result itself effectively changing the 
     6   * invocation calling site.
     7   * 
     8   * @param invocationContext target command context
     9   * @return the invocation target 
    10   */
    11  ParseResult beforeInvocation(ParseResult invocationContext);
    12 
    13  /**
    14   * Method called after successfully invoking the target command (described by 
    15   * {@link ParseResult}).
    16   * 
    17   * @param invocationContext target command context
    18   * @param result the invocation result
    19   */
    20  void afterReturningInvocation(ParseResult invocationContext, Object result);
    21 
    22  /**
    23   * Method called after invoking the target command (described by {@link ParseResult}) 
    24   * had thrown an exception .
    25   * 
    26   * @param invocationContext target command context
    27   * @param thrown the thrown object
    28   */
    29  void afterThrowingInvocation(ParseResult invocationContext, Throwable thrown);
    30 
    31 }


    1.6 命令行选项
      在启动shell时,可以指定一些命令行选项。它们是:

    • --profiles - 指定spring.profiles系统属性的值,激活了Spring 3.1和更大的概要支持。
    • --cmdfile - 指定一个文件读取它包含shell命令
    • --histsize - 指定存储在命令历史文件的最大数量行,默认值是3000。
    • --disableInternalCommands - 在注册命令前禁用所有命令,可以通过在您的shell插件文件中引用它们来选择性地添加任何内部命令。看看Spring Shell javadocs特定命令位于org.springframework.shell.commands包以及内建命令的部分在这个文档。

    1.7 脚本和注释
      可以通过在命令--cmdfile 来启动或执行脚本命令。使用脚本有助于添加注释,这可以块注释的/**/,或行注释//; 。

      

      其他相关链接:

      《Spring Shell介绍》http://www.cnblogs.com/acm-bingzi/p/springshell.html

      《开发Spring Shell应用程序》http://www.cnblogs.com/acm-bingzi/p/springshell2.html

  • 相关阅读:
    idea 将java导出为可执行jar及导入jar依赖
    使用idea 调试java -jar xxx.jar方式启动
    springboot 打成的jar包在ClassLoader().getResource方法读取文件为null
    maven 使用dependencyManagement统一管理依赖版本
    Win10系列:C#应用控件基础5
    Win10系列:C#应用控件基础4
    Win10系列:C#应用控件基础3
    Win10系列:C#应用控件基础2
    Win10系列:C#应用控件基础1
    Win10系列:UWP界面布局进阶9
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/springshell1.html
Copyright © 2020-2023  润新知