无法将DEFAULT_STREAMS_CONFIG_BEAN_NAME标记为主要

时间:2019-03-26 14:28:48

标签: spring-boot spring-kafka

我刚刚升级到spring-boot 2.1.3 RELEASE,由于这个新的类/方法(kafkaStreamsFactoryBeanConfigurer仅需要一个factoryBean),所以我不能拥有多个StreamsBuilderFactoryBean:

@Configuration
@ConditionalOnClass(StreamsBuilder.class)
@ConditionalOnBean(name = 
KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME)
class KafkaStreamsAnnotationDrivenConfiguration {
 //...
 @Bean
 public KafkaStreamsFactoryBeanConfigurer kafkaStreamsFactoryBeanConfigurer(
        StreamsBuilderFactoryBean factoryBean) {
    return new KafkaStreamsFactoryBeanConfigurer(this.properties, factoryBean);
 }
}

我收到此错误:

Parameter 0 of method kafkaStreamsFactoryBeanConfigurer in org.springframework.boot.autoconfigure.kafka.KafkaStreamsAnnotationDrivenConfiguration required a single bean, but 2 were found:
- &defaultKafkaStreamsBuilder: defined by method 'defaultKafkaStreamsBuilder' in class path resource [com/elsevier/q2c/transaction/snapshot/builder/config/KafkaStreamsConfig.class]
- &snapshotKafkaStreamsBuilder: defined by method 'snapshotKafkaStreamsBuilder' in class path resource [com/elsevier/q2c/transaction/snapshot/builder/config/KafkaStreamsConfig.class]

我希望通过将两个StreamsBuilderFactoryBean中的一个标记为@Primary(如拟议的here)来解决此问题。但是即使我这样做:

@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME)
@Primary
public StreamsBuilderFactoryBean defaultKafkaStreamsBuilder(
        @Qualifier(KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME) KafkaStreamsConfiguration streamsConfiguration) {
    StreamsBuilderFactoryBean streamsBuilderFactoryBean = new StreamsBuilderFactoryBean(streamsConfiguration);
    return streamsBuilderFactoryBean;
}

@Bean(name = SNAPSHOT_STREAMS_BUILDER_BEAN_NAME)
public StreamsBuilderFactoryBean snapshotKafkaStreamsBuilder(
        @Qualifier(SNAPSHOT_STREAMS_CONFIG_BEAN_NAME) KafkaStreamsConfiguration streamsConfiguration) {
    StreamsBuilderFactoryBean streamsBuilderFactoryBean = new StreamsBuilderFactoryBean(streamsConfiguration);
    return streamsBuilderFactoryBean;
}

仍然出现完全相同的错误。

我认为也许@Primary使构造的bean成为Primary。任何帮助都不胜感激!

编辑:我通过删除@EnableKafkaStreamsDEFAULT_STREAMS_BUILDER_BEAN_NAME来解决此问题。结果,KafkaStreamsFactoryBeanConfigurer无法加入。

2 个答案:

答案 0 :(得分:0)

我不确定@Primary为什么不起作用。我打开了GitHub issue

答案 1 :(得分:0)

就我而言,我通过删除@EnableKafkaStreams来解决该问题(即不会自动创建任何流bean),然后手动创建所有需要的豆。如果流不止一个,看起来会更好,因为命名也可能更具破坏性。