註解

在方法上使用 @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 umbrella 的其他部分相似,並為使用者提供了更好的彈性,使其能夠包含而非排除命令目標。

您可以使用 @EnableCommand 定義目標類別。它將從所有 Configuration 類別中選取。

@EnableCommand(Example.class)
class App {
}

您可以使用 @CommandScan 定義目標類別。它將從所有 Configuration 類別中選取。

在頂層 Spring Boot App 類別中定義 @CommandScan,它將自動掃描 App 下所有套件和類別中的所有命令目標。
@CommandScan
class App {
}