程式化
在程式化模型中,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
};
}