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接口如下所示:
public interface ExecutionProcessor extends CommandMarker {
/**
* Method called before invoking the target command (described by {@link ParseResult}).
* Additionally, for advanced cases, the parse result itself effectively changing the
* invocation calling site.
*
* @param invocationContext target command context
* @return the invocation target
*/
ParseResult beforeInvocation(ParseResult invocationContext);
/**
* Method called after successfully invoking the target command (described by
* {@link ParseResult}).
*
* @param invocationContext target command context
* @param result the invocation result
*/
void afterReturningInvocation(ParseResult invocationContext, Object result);
/**
* Method called after invoking the target command (described by {@link ParseResult})
* had thrown an exception .
*
* @param invocationContext target command context
* @param thrown the thrown object
*/
void afterThrowingInvocation(ParseResult invocationContext, Throwable thrown);
}
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
Spring Shell参考文档的更多相关文章
- 教您怎么从spring 官网下载参考文档
假如您使用spring,那么本经验可能帮助到您. 假如您使用spring的过程中,需要查询一些文档,那么本经验可能帮助到您. 假如您对下载spring的文档有疑惑,那么本经验可能帮助到您. 教您怎么从 ...
- redis参考文档
本文为之前整理的关于redis的文档,放到博客上一份,也方便我以后查阅. redis简介 Redis是一个开源的.高性能的.基于键值对的缓存与存储系统, 通过提供多种键值数据类型来适应不同场景下的缓存 ...
- 微信小程序 不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html
微信小程序 不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html 友情提示: 大家 ...
- Web Api 在线参考文档
参考文档: https://developer.mozilla.org/zh-CN/docs/Web/API
- 让IE8兼容问题,参考文档bootstrap
问题:制作的页面基本没啥问题,只有IE8不好使 参考文档:bootstrap官网 方案一 方案二
- nexus 参考文档
参考文档: http://books.sonatype.com/nexus-book/reference/index.html E:\e\nexus\nexus-2.12.0-01\conf\nexu ...
- Oracle官网下载参考文档
最近有人问我有没有Oracle11g数据库官方参考文档,我就想,这不是在官网可以下载到的吗,疑惑,问了之后才知道,他官网找过,但时没有找到.不要笑,其实有很多人一样是找不到的,下面就一步一步操作下: ...
- Qt5.11参考文档
Qt5.11参考文档: http://www.bim-times.com/qt/Qt-5.11.1/qtdoc/index.html (来源于Qt官网)
- RxJava 参考文档
/*************************************************************** * RxJava 参考文档 * 说明: * 最近无意中发现RxJava ...
随机推荐
- day38
今日内容: 1.认识数据库 2.修改默认密码 3.常用操作指令 1.认识数据库 什么是MYSQL? 是一个关系型数据库管理系统,基于socket编写的C/S架构的软件 什么是数据库? 数据:用于记录事 ...
- mysql删除关联表数据
DELETE og,oiFROM order_goods og, order_info oiWHERE oi.order_id = og.order_id and oi.user_id = 6
- Java关键字(二)——native
本篇博客我们将介绍Java中的一个关键字——native. native 关键字在 JDK 源码中很多类中都有,在 Object.java类中,其 getClass() 方法.hashCode()方法 ...
- HUE配置HBase
HBase的配置 修改配置hue.ini的配置文件 [hbase] hbase_clusters=(Cluster|node1:) hbase_conf_dir=/usr/hbase-0.98.12. ...
- 2017-2018-1 20155202 张旭 嵌入式C语言——时钟提取时分秒
2017-2018-1 20155202 张旭 嵌入式C语言--时钟提取时分秒 任务要求: 在作业本上完成附图作业,要认真看题目要求. 提交作业截图 作弊本学期成绩清零(有雷同的,不管是给别人传答案, ...
- WPF编程,通过【帧】动态更改控件属性的一种方法。
原文:WPF编程,通过[帧]动态更改控件属性的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/detail ...
- 《图说VR入门》——入门汇总
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/53818922 作者:car ...
- python 回溯法 记录
一直不是太理解回溯法,这几天集中学习了一下,记录如下. 回溯法有"通用的解题法"之称. 1.定义: 也叫试探法,它是一种系统地搜索问题的解的方法. 2.基本思想: 从一条路往前 ...
- Eclipse中Hadoop插件配置
Eclipse中Hadoop插件DFS配置 http://www.cnblogs.com/xia520pi/archive/2012/05/20/2510723.html
- 前端页面loading效果(CSS实现)
当首页内容或图片比较多时,加载时间会比较长,此时可能出现页面白屏的情况,用户体验较差.所以,在页面完全加载出来之前,可以考虑加入loading效果,当页面完全加载完后,是loading消失即可. 1. ...