Annotation

@Command 注解被用于某个方法时,它将该方法标记为命令注册的候选方法。在下例中,命令 example 被定义。

link:../../test/java/org/springframework/shell/docs/CommandAnnotationSnippets.java[role=include]

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

link:../../test/java/org/springframework/shell/docs/CommandAnnotationSnippets.java[role=include]

使用 @Command 不会自动注册命令目标,而是需要使用 @EnableCommand 和/或 @CommandScan 注解。此模型与 Spring 伞形组织的其他部分的模型类似,并且为用户提供了更好的灵活性,对命令目标而言是包容性的,而不是排他性的。

你可以使用 @EnableCommand 来定义目标类。它可以从所有 Configuration 类中提取。

link:../../test/java/org/springframework/shell/docs/CommandAnnotationSnippets.java[role=include]

你可以使用 @CommandScan 来定义目标类。它可以从所有 Configuration 类中提取。

在顶层 on App 类中定义 @CommandScan in Spring Boot App,它将自动扫描所有包和类中的所有命令目标。

link:../../test/java/org/springframework/shell/docs/CommandAnnotationSnippets.java[role=include]