Define Bean in combination with @ConditionalOnMissingBean does not work since Spring Boot Upgrade

Norbert Koch :

I am using the Spring Boot Starter Version 2.0.8.RELEASE and try to upgrade to: 2.1.4.RELEASE. I got the following error:

The bean 'defaultKafkaStreamsConfig', defined in class path resource [org/springframework/boot/autoconfigure/kafka/KafkaStreamsAnnotationDrivenConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/mycompany/stream/configuration/StreamsConfiguration.class] and overriding is disabled.

The Bean is defined as:

public class StreamsConfiguration {

    @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
    public StreamsConfig kStreamsConfigs(StreamsConfigFactory factory) {
        Map<String, Object> additionalProperties = new HashMap<>();
        addConfigurationProperties(additionalProperties);

        return factory.build(additionalProperties);
    }
}

The bean in the Spring Kafka Library has the annotation: @ConditionalOnMissingBean.

I tried it already by annotate my Bean with: @AutoConfigureBefore and @Primary.

What was changed in Spring Boot, that the context loading now differs from versions before?

This is just an easy example, This happens on many parts in my code.

Btw.: I use already @EnableAutoConfigure.

Norbert Koch :

I found the cause.

I had to change the return type from StreamsConfig to KafkaStreamsConfiguration.

The Annotation: @ConditionalOnMissingBean does not working, if the target class differs with its type from the already loaded bean.

The error message is misleading, because the message just mean the bean name and not the bean type.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=105091&siteId=1