Two beans with the same name results in ConflictingBeanDefinitionException despite using @Primary

10,709

@Primary has no relation to bean names. The javadoc states

Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency.

This only applies to a context containing two beans of some type A where a bean of type B requires an A to be injected. The A bean annotated with @Primary will have priority.

Bean ids/names for a given type must be unique. (Bean definitions with the same name can overwrite each other for the same bean type. For example, this would happen if you component scanned a class annotated with @Component but also provided a @Bean method for that same type.)

Share:
10,709
fracz
Author by

fracz

Father. Biker. Code quality enthusiast. Write less code. The less code the fewer bugs. https://supla.org https://gitexercises.fracz.com

Updated on June 04, 2022

Comments

  • fracz
    fracz about 2 years

    I have an application initializer class that is used to insert application specific data to database.

    @Component("applicationInitializer")
    public class ApplicationInitializer {
        @PostConstruct
        public void init(){
            // some clever code here
        }
    }
    

    There is also DevApplicationInitializer class that is used to initialize database with some sample data on developer machine (this class is excluded when deploying production code).

    @Component("applicationInitializer")
    @Primary
    public class DevApplicationInitializer extends ApplicationInitializer {
        @PostConstruct
        @Override
        public void init(){
            super.init();
            // even more clever code here
        }
    }
    

    Until I have given a name for the beans (only the @Component annotations) - everything worked fine - when DevApplicationInitializer was available, it was instantiated instead of ApplicationInitializer. When I gave them an applicationInitializer name, the exception is being thrown:

    org.springframework.context.annotation.ConflictingBeanDefinitionException: 
    Annotation-specified bean name 'applicationInitializer' for bean class
    [com.example.DevApplicationInitializer] conflicts with existing, non-compatible 
    bean definition of same name and class [com.example.ApplicationInitializer]
    

    Why the @Primary annotation is not respected when beans have name? I need them to have one, because I ensure in other place that the initializer has been instantiated with @DependsOn("applicationInitializer") annotation.