コマンドカタログ

CommandCatalog インターフェースは、コマンド登録がシェルアプリケーションにどのように存在するかを定義します。コマンドを動的に登録および登録解除することが可能であり、シェルの状態に応じて可能なコマンドが出入りするユースケースに柔軟性を与えます。次の例を検討してください。

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 の現在の制限は、コマンドが解決されるたびに使用されることです。コマンド解決の呼び出しに時間がかかる場合は使用しないことをお勧めします。シェルの動作が遅くなるからです。

コマンドカタログカスタマイザー

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 が処理します。