在多活页夹应用中定制活页夹

当应用程序中有多个活页夹并希望自定义活页夹时,可以通过提供BinderCustomizer实现。 对于使用单一活页夹的应用,这个特殊定制器并非必需,因为活页夹上下文可以直接访问自定义豆。 然而,在多活页夹场景中情况并非如此,因为不同的活页夹存在于不同的应用环境中。 通过提供BinderCustomizer界面上,虽然绑定器存在于不同的应用环境中,但将获得定制化。 Spring Cloud Stream 确保定制在应用开始使用活页夹之前完成。 用户必须检查活页夹类型,然后应用必要的自定义设置。spring-doc.cadn.net.cn

这里有一个提供BinderCustomizer豆。spring-doc.cadn.net.cn

@Bean
public BinderCustomizer binderCustomizer() {
    return (binder, binderName) -> {
        if (binder instanceof KafkaMessageChannelBinder kafkaMessageChannelBinder) {
            kafkaMessageChannelBinder.setRebalanceListener(...);
        }
        else if (binder instanceof KStreamBinder) {
            ...
        }
        else if (binder instanceof RabbitMessageChannelBinder) {
            ...
        }
    };
}

请注意,当有多个同类型活页夹实例时,可以用活页夹名称来过滤自定义。spring-doc.cadn.net.cn