注解

当在方法上使用 @Command 注解时,它将该方法标记为命令注册的候选。在以下示例中,定义了一个命令 example

class Example {

	@Command(command = "example")
	public String example() {
		return "Hello";
	}
}

@Command 注解可以放置在一个类上,该类定义了在同一类中定义的 @Command 方法的默认值或共享设置。在以下示例中,定义了一个命令 parent example

@Command(command = "parent")
class Example {

	@Command(command = "example")
	public String example() {
		return "Hello";
	}
}

使用 @Command 不会自动注册命令目标,而是需要使用 @EnableCommand 和/或 @CommandScan 注解。这种模式在 Spring 家族的其他部分中很常见,并为用户提供了更好的灵活性,使其对命令目标更具包容性而非排他性。

您可以使用 @EnableCommand 定义目标类。它将从所有 Configuration 类中获取。

@EnableCommand(Example.class)
class App {
}

您可以使用 @CommandScan 定义目标类。它将从所有 Configuration 类中获取。

在 Spring Boot 的顶层 App 类中定义 @CommandScan,它将自动扫描 App 下所有包和类中的所有命令目标。
@CommandScan
class App {
}
© . This site is unofficial and not affiliated with VMware.