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