命令目录
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;
}
}
important
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 会处理其余部分。