对于最新的稳定版本,请使用 Spring Shell 3.3.3spring-doc.cn

未找到命令

默认情况下,通过处理缺少的命令并输出一条简单的消息:CommandNotFoundResultHandlerspring-doc.cn

shell:>missing
No command found for 'missing'

内部使用 this 是一个简单的函数,接受 a 并返回一个文本 消息。下面是自定义消息提供程序的示例。CommandNotFoundResultHandlerCommandNotFoundMessageProviderProviderContextspring-doc.cn

class CustomProvider implements CommandNotFoundMessageProvider {

	@Override
	public String apply(ProviderContext context) {
		// parsed commands without options
		List<String> commands = context.commands();
		// actual error, usually CommandNotFound exception
		Throwable error = context.error();
		// access to registrations at this time
		Map<String, CommandRegistration> registrations = context.registrations();
		// raw text input from a user
		String text = context.text();
		return "My custom message";
	}
}

可以通过将其定义为 bean 来更改此实现。spring-doc.cn

@Bean
CommandNotFoundMessageProvider provider1() {
	return new CustomProvider();
}

CommandNotFoundResultHandler是一个功能接口,因此它可以 作为 lambda 编写。spring-doc.cn

@Bean
CommandNotFoundMessageProvider provider2() {
	return ctx -> "My custom message";
}