命令目录

CommandCataloginterface 定义命令注册在 一个 shell 应用程序。可以动态注册和取消注册 commands,为可能的用例提供了灵活性 命令 来来去去,具体取决于 shell 的状态。请考虑以下示例:spring-doc.cadn.net.cn

CommandRegistration registration = CommandRegistration.builder().build();
catalog.register(registration);

命令解析器

您可以实现CommandResolver接口并定义一个 Bean 以动态 解析从命令名称到其CommandRegistration实例。考虑 以下示例:spring-doc.cadn.net.cn

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 感觉迟钝。

命令目录定制器

您可以使用CommandCatalogCustomizerinterface 自定义CommandCatalog. 它的主要用途是修改目录。此外,在spring-shellauto-configuration,则 interface 用于注册现有的CommandRegistrationbean 导入到目录中。 请考虑以下示例:spring-doc.cadn.net.cn

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 会处理其余部分。spring-doc.cadn.net.cn