【问题标题】:Spring ConflictingBeanDefinitionException conflicts with existing, non-compatible bean definition of same name and classSpring ConflictingBeanDefinitionException 与同名和类的现有不兼容 bean 定义冲突
【发布时间】:2018-03-13 07:47:23
【问题描述】:

我遇到与this 相同的问题。我知道如何解决这个问题,但我不知道为什么会这样。

正如this 提到的,默认情况下弹簧注入是by type,而不是by name。这就是我们可以将实现注入接口的原因。

在我的spring web应用程序中,我正在注入相应的接口,在按类型解析时,它应该找到确切的具体类,但我遇到异常ConflictingBeanDefinitionException

interface A;
interface B;

Class com.level1.Child implements A;
Class com.level2.Child implements B;

@Autowired
private B b; // expecting com.level2.Child obviously!

怎么会?有没有更好的方法来解决这个问题?这很烦人,因为我需要在很多变量上注释Qualifier,因为我的应用程序的不同包中有许多相同的类名。

【问题讨论】:

  • 您能否提供有关如何在应用程序中实例化 bean 的更多详细信息:with .xml | @Components 通过包扫描 |作为@Beans ?

标签: java spring dependency-injection


【解决方案1】:

如果定义了多个同名的bean,那么后面定义的将覆盖前面定义的。

我建议您将 Childs 重命名为 ChildAChildB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2019-08-10
    • 2017-09-12
    • 2013-08-08
    • 2021-09-01
    相关资源
    最近更新 更多