命令目錄

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 會處理剩下的事情。