该版本仍在开发中,尚未被视为稳定。最新稳定版本请使用Spring Shell 3.4.1spring-doc.cadn.net.cn

找不到指令

默认情况下,缺失命令通过以下方式处理命令非找到结果处理器并输出一个简单的消息:spring-doc.cadn.net.cn

shell:>missing
No command found for 'missing'

内部命令非找到结果处理器是使用命令非找到消息提供者这是一个简单的函数,取提供者上下文并回复短信 消息。以下是自定义消息提供商可能的示例。spring-doc.cadn.net.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";
	}
}

可以通过将其定义为“豆子”来改变这种实现。spring-doc.cadn.net.cn

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

命令非找到结果处理器是一个功能性接口,因此它可以 写作者为一个lambda。spring-doc.cadn.net.cn

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