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