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

你离开我真会死。 提交于 2019-12-07 12:19:09

问题


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.


回答1:


@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.)



来源:https://stackoverflow.com/questions/24066425/two-beans-with-the-same-name-results-in-conflictingbeandefinitionexception-despi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!