尽管使用了@Primary,但两个具有相同名称的bean会导致ConflictingBeanDefinitionException [英] Two beans with the same name results in ConflictingBeanDefinitionException despite using @Primary

查看:3990
本文介绍了尽管使用了@Primary,但两个具有相同名称的bean会导致ConflictingBeanDefinitionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序初始化程序类,用于将特定于应用程序的数据插入数据库。

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
    }
}

还有 DevApplicationInitializer 类,用于初始化数据库开发人员计算机上的示例数据(在部署生产代码时排除此类)。

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
    }
}

直到我给bean命名(只有 @Component 注释) - 一切正常 - 当 DevApplicationInitializer 是可用,它被实例化而不是 ApplicationInitializer 。当我给他们一个 applicationInitializer 名称时,抛出了异常:

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]

为什么 @当bean具有名称时,不会遵守主注释?我需要他们有一个,因为我在其他地方确保初始化程序已经使用 @DependsOn(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.

推荐答案

@Primary 与bean名称无关。 javadoc状态

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


表示当多个
候选者有资格自动装配单值依赖项时,应优先考虑bean。

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

这仅适用于包含两种类型的bean的上下文 A 其中类型 B 的bean需要注入 A 。用 @Primary 注释的A bean将具有优先权。

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 ID /名称必须是唯一的。 (具有相同名称的Bean定义可以为相同的bean类型相互覆盖。例如,如果组件扫描了使用 @Component 注释的类,则会发生这种情况。 @Bean 同一类型的方法。)

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

这篇关于尽管使用了@Primary,但两个具有相同名称的bean会导致ConflictingBeanDefinitionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆