命令目錄
CommandCatalog 介面定義了命令註冊在 shell 應用程式中如何存在。可以動態註冊和登出命令,這為可能根據 shell 狀態而出現和消失的用例提供了靈活性。考慮以下示例
CommandRegistration registration = CommandRegistration.builder().build();
catalog.register(registration);
命令解析器
您可以實現 CommandResolver 介面並定義一個 bean,以動態解析從命令名稱到其 CommandRegistration 例項的對映。考慮以下示例
static class CustomCommandResolver implements CommandResolver {
List<CommandRegistration> registrations = new ArrayList<>();
CustomCommandResolver() {
CommandRegistration resolved = CommandRegistration.builder()
.command("resolve command")
.build();
registrations.add(resolved);
}
@Override
public List<CommandRegistration> resolve() {
return registrations;
}
}
CommandResolver 當前的限制是它在每次解析命令時都會使用。因此,如果命令解析呼叫花費很長時間,我們建議不要使用它,因為它會使 shell 感覺遲鈍。 |
命令目錄定製器
您可以使用 CommandCatalogCustomizer 介面來自定義 CommandCatalog。它的主要用途是修改目錄。此外,在 spring-shell 自動配置中,此介面用於將現有的 CommandRegistration bean 註冊到目錄中。考慮以下示例
static class CustomCommandCatalogCustomizer implements CommandCatalogCustomizer {
@Override
public void customize(CommandCatalog commandCatalog) {
CommandRegistration registration = CommandRegistration.builder()
.command("resolve command")
.build();
commandCatalog.register(registration);
}
}
您可以將 CommandCatalogCustomizer 建立為一個 bean,Spring Shell 將處理其餘部分。