命令目錄
CommandCatalog
介面定義了命令註冊在 shell 應用程式中的存在方式。可以動態地註冊和登出命令,這為根據 shell 狀態變化而出現和消失的用例提供了靈活性。考慮以下示例
CommandRegistration registration = CommandRegistration.builder().build();
catalog.register(registration);
Command Resolver
你可以實現 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 感覺遲鈍。 |
Command Catalog Customizer
你可以使用 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 會處理其餘的事情。