命令目录
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 会处理其余的事情。