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 ...
随机推荐
- Redis Replication
Replication 官网说明:http://www.redis.io/topics/replication Redis使用异步复制; 一个Master可以有多个Slaves; Slaves可以接收 ...
- win7中mysql安装
最近需要用到MySQL,从官网上下载了一个安装文件,但是安装时一直弹出如下提示信息: Configuration of MySQL Server 5.7 is taking longer than e ...
- Django Rest Framework源码剖析(六)-----序列化(serializers)
一.简介 django rest framework 中的序列化组件,可以说是其核心组件,也是我们平时使用最多的组件,它不仅仅有序列化功能,更提供了数据验证的功能(与django中的form类似). ...
- 20155202张旭 Exp5 MSF基础应用
20155202张旭 Exp5 MSF基础应用 实践内容 本次实验我使用的攻击方式: 1.针对office软件的主动攻击:--MS10-087: 2.MS10-002漏洞对浏览器攻击 3.针对客户端的 ...
- 20155206赵飞 Exp1PC平台逆向破解及Bof基础实践
实验一 逆向及Bof基础 1.掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP汇编指令的机器码是"90" JNE汇编指令的机器码是"75" ...
- Centos 定时任务发送smtp邮件
接着上一篇文章...... 1.首先创建一个sheel的脚本命令,我是在home文件夹下面创建的命令: touch a.sh 2.编辑a.sh脚本 vim a.sh ,键入键盘 i 键 准备插入 ...
- JavaScript快速入门-ECMAScript对象介绍
一.概念介绍 在 ECMAScript 中,所有对象并非同等创建的.一般来说,可以创建并使用的对象有三种:本地对象.内置对象和宿主对象. 要理解这三种对象,先要理解宿主环境. 1.宿主环境 一般宿主环 ...
- JavaScript快速入门-实战(例子)
1.模拟bootstrap中的模态框 效果图:(点我后,弹出对话框,最下面的内容可以看到,但是有一定的透明度.) 思路分析: 整体分为三层,最底层(点我),中间透明层(实现透明效果),最顶层(最新内容 ...
- 设计模式 笔记 抽象工厂模式 Abstract Factory
//---------------------------15/04/09---------------------------- //Abstract Factory 抽象工厂----对象创建型模式 ...
- 【原创】梵高油画用深度卷积神经网络迭代10万次是什么效果? A neural style of convolutional neural networks
作为一个脱离了低级趣味的码农,春节假期闲来无事,决定做一些有意思的事情打发时间,碰巧看到这篇论文: A neural style of convolutional neural networks,译作 ...