程式設計式
在程式設計模型中,可以將 CommandRegistration 定義為 @Bean,它將自動註冊。
@Bean
CommandRegistration commandRegistration() {
return CommandRegistration.builder()
.command("mycommand")
.build();
}
如果所有命令都有共同之處,則會建立一個 CommandRegistration.BuilderSupplier 例項,該例項可以自動裝配。此供應商的預設實現返回一個新的構建器,因此您無需擔心其內部狀態。
| 以程式設計方式註冊的命令會自動新增幫助選項中提及的幫助選項。 |
如果定義了此供應商型別的 bean,則自動配置將回退,為您提供重新定義預設功能的選項。
@Bean
CommandRegistration commandRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command("mycommand")
.build();
}
如果您希望集中修改供應商提供的構建器例項,可以定義 CommandRegistrationCustomizer bean,如下所述
@Bean
CommandRegistrationCustomizer commandRegistrationCustomizerExample() {
return builder -> {
// customize instance of CommandRegistration.Builder
};
}