2 開發Spring Shell應用程序
向shell提供命令非常簡單,需要學習的注解很少。該命令的實現風格與使用依賴注入的應用程序的開發類相同,您可以利用Spring容器的所有特性來實現您的命令類。
2.1 標記接口
創建命令的第一步是實現標記接口CommandMarker,並使用Spring的@Component注解對類進行注解(注意一個JIRA問題:提供@CliCommand元注解避免使用標記接口)。使用helloworld示例應用程序中的代碼為例,HelloWorldCommands類的代碼如下所示:
1 @Component 2 public class HelloWorldCommands implements CommandMarker { 3 4 // use any Spring annotations for Dependency Injection or other Spring interfaces 5 // as required. 6 7 // methods with @Cli annotations go here 8 9 }
2.2 日志
目前,日志記錄是使用JDK日志記錄的。由於控制台、JLine和Ansi處理的復雜性,一般都建議將消息作為返回值的方式顯示在shell窗口。但是,當需要進行日志記錄時,典型的JDK logger聲明就足夠了。
1 @Component 2 public class HelloWorldCommands implements CommandMarker { 3 4 protected final Logger LOG = Logger.getLogger(getClass().getName()); 5 6 // methods with @Cli annotations go here 7 8 }
注意:一般開發人員的職責是為第三方庫處理日志,應當要減少日志級別,這樣控制台或者shell窗口就不會受到日志消息的影響。
2.3 CLI注解
在方法和方法參數上使用了三個注釋,這些注釋定義了與shell交互的主要契約,分別是:
- CliAvailabilityIndicator - 放在一個方法前返回一個布爾值,表示在shell中是否可以執行一個特定的命令。這個決定通常基於之前執行的命令的歷史。它可以防止在滿足某些先決條件時出現外部命令,例如執行'configuration'命令。
- CliCommand - 放置在向shell提供命令的方法上。它的值提供了一個或多個字符串,這些字符串作為特定命令名的開始。在整個應用程序中,包括所有的插件中這些必須是唯一的。
- CliOption - 放置在命令方法的參數中,允許它默認值聲明參數值為必填的或可選的。
下面是在命令類中使用這些注解的簡單用法
1 @Component 2 public class HelloWorldCommands implements CommandMarker { 3 4 @CliAvailabilityIndicator({"hw simple"}) 5 public boolean isCommandAvailable() { 6 return true; 7 } 8 9 @CliCommand(value = "hw simple", help = "Print a simple hello world message") 10 public String simple( 11 @CliOption(key = { "message" }, mandatory = true, help = "The hello world message") 12 final String message, 13 14 @CliOption(key = { "location" }, mandatory = false, 15 help = "Where you are saying hello", specifiedDefaultValue="At work") 16 final String location) { 17 18 return "Message = [" + message + "] Location = [" + location + "]"; 19 20 } 21 }
注解@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()字符。
2.4 測試shell命令
執行測試的shell命令,您可以實例化shell在一個測試用例中執行命令,然后在返回值CommandResult執行斷言。一個簡單的基類設置如下所示:
1 public abstract class AbstractShellIntegrationTest { 2 3 private static JLineShellComponent shell; 4 5 @BeforeClass 6 public static void startUp() throws InterruptedException { 7 Bootstrap bootstrap = new Bootstrap(); 8 shell = bootstrap.getJLineShellComponent(); 9 } 10 11 @AfterClass 12 public static void shutdown() { 13 shell.stop(); 14 } 15 16 public static JLineShellComponent getShell() { 17 return shell; 18 } 19 20 }
這里有一個測試日期命令的例子:
1 public class BuiltInCommandTests extends AbstractShellIntegrationTest { 2 3 @Test 4 public void dateTest() throws ParseException { 5 6 //Execute command 7 CommandResult cr = getShell().executeCommand("date"); 8 9 //Get result 10 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US); 11 Date result = df.parse(cr.getResult().toString()); 12 13 //Make assertions - DateMaters is an external dependency not shown here. 14 Date now = new Date(); 15 MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result)); 16 } 17 }
CommandResult的getResult方法返回的 java.lang.Class將匹配與@CliCommand注解方法的返回值。您應該向適當的類型轉換,以幫助執行您的斷言。
2.5 構建和運行shell
在我們看來,構建和執行shell最簡單的方法是剪切和粘貼腳本。這將使來自於分級的應用程序插件創建一個bin目錄,該目錄帶有用於windows和Unix的啟動腳本,並將所有依賴jar放在lib目錄中。Maven有一個類似的插件——AppAssembler插件。
shell的主類是org.springframework.shell.Bootstrap的。只要您在類路徑上放置其他的插件,或者是獨立開發的,引導類就會將它們合並到shell中。
其他相關鏈接:
《Spring Shell介紹》http://www.cnblogs.com/acm-bingzi/p/springshell.html
《Spring Shell參考文檔》http://www.cnblogs.com/acm-bingzi/p/springshell1.html
