註解

當在方法上使用 @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.