跳到主要内容

编程式

QWen Max 中英对照 Programmatic

在编程模型中,CommandRegistration 可以定义为一个 @Bean,它将被自动注册。

@Bean
CommandRegistration commandRegistration() {
return CommandRegistration.builder()
.command("mycommand")
.build();
}
java

如果你的所有命令都有共同点,那么会创建一个 CommandRegistration.BuilderSupplier 的实例,该实例可以被自动装配。这个供应商的默认实现返回一个新的构建器,因此你不需要担心其内部状态。

important

通过编程方式注册的命令会自动添加在帮助选项中提到的帮助选项

如果定义了此供应商类型的 bean,则自动配置将会回退,从而让你有机会重新定义默认功能。

@Bean
CommandRegistration commandRegistration(CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command("mycommand")
.build();
}
java

CommandRegistrationCustomizer beans 可以在你需要集中修改上述供应商提供的构建器实例时定义。

@Bean
CommandRegistrationCustomizer commandRegistrationCustomizerExample() {
return builder -> {
// customize instance of CommandRegistration.Builder
};
}
java