spring 自动装配问题 & 没有独特的 bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2699608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:03:15  来源:igfitidea点击:

Problem with Autowiring & No unique bean

spring

提问by mada

I have 2 classes (B,C) extends class A.

我有 2 个班级 (B,C) 扩展了班级 A。

@Service
public class A  extends AbstratClass<Modele>{

    @Autowired
    A(MyClass  br) {
        super(br);
    }


@Service
public class B  extends A{

  @Autowired
  B (MyClass  br) {
     super(br);
  }



@Service
public class C  extends A{

  @Autowired
  C (MyClass  br) {
     super(br);
  }

But i have this message:

但我有这个消息:

No unique bean of type [A] ] is defined: expected single matching bean but found 2: [A, B, moveModeleMarshaller]

没有定义类型 [A] 的唯一 bean:预期的单个匹配 bean,但发现 2:[A, B, moveModeleMarshaller]

I really cant get why i have this message & how to resolve even after reading Spring documentation.

我真的不明白为什么我会收到这条消息以及如何在阅读 Spring 文档后解决。

Thanks in advance.

提前致谢。

回答by Espen

You should rewrite your class to something like this with the @Qualifierannotation.

您应该使用@Qualifier注释将类重写为类似的内容。

@Service
@Qualifier("a")
public class A  extends AbstratClass<Modele>{

    @Autowired
    A(MyClass  br) {
        super(br);
    }


@Service
@Qualifier("b")
public class B  extends A{

  @Autowired
  B (MyClass  br) {
     super(br);
  }

@Service
@Qualifier("c")
public class C  extends A{

  @Autowired
  C (MyClass  br) {
     super(br);
  }

You must also use the @Qualifier annotation on the instance of type A you're autowiring the Spring bean into.

您还必须在将 Spring bean 自动装配到的类型 A 的实例上使用 @Qualifier 注释。

Something like this:

像这样的东西:

public class Demo {

    @Autowired
    @Qualifier("a")
    private A a;

    @Autowired
    @Qualifier("b")
    private A a2;

    public void demo(..) {..}
}

If you don't like to have this Spring configuration in your production code, you have to write the dependency injection logic with XML or Java configuration instead.

如果你不喜欢在你的生产代码中有这个 Spring 配置,你必须用 XML 或 Java 配置来编写依赖注入逻辑。

You can also specify a default bean of type A with the @Primaryannotation above one of your service classes that extends type A. Then Spring can autowire without specifying the @Qualifierannotation.

您还可以使用@Primary扩展类型 A 的服务类之一上方的注释指定类型 A 的默认 bean 。然后 Spring 可以在不指定@Qualifier注释的情况下自动装配。

Since Spring will never try to guess which bean to inject, you have to specify which one or mark one of them with @Primaryas long as its more than one bean of a type.

由于 Spring 永远不会尝试猜测要注入哪个 bean,因此您必须指定哪个 bean 或标记其中@Primary一个 bean,只要它是一种类型的多个 bean。

回答by Bozho

You are trying (somewhere else) to autowire a bean of type A. Something like:

您正在尝试(在其他地方)自动装配类型为 的 bean A。就像是:

@Autowired
private A beanA;

But you have 2 beans that conform to this.

但是您有 2 个符合此条件的 bean。

You can resolve this by using @Resourceand specifying which bean exactly:

您可以通过使用@Resource并准确指定哪个 bean来解决此问题:

@Resource("b")
private A beanA;

(where "b" is the name of the injected bean) or using the @Qualifierannotation.

(其中“b”是注入 bean 的名称)或使用@Qualifier注解。

回答by Suryaprakash Pisay

Generally you will get this error when defined two beans with same class

通常在定义两个具有相同类的 bean 时会出现此错误

<bean id="a" class="com.package.MyClass"/>
<bean id="b" class="com.package.MyClass"/>

if you address the above two line we have two beans with same class.

如果您解决上述两行,我们将有两个具有相同类的 bean。

when you trying to autowire this class in any other classed you will get this type of error

当您尝试在任何其他类中自动装配此类时,您将收到此类错误

You have two solutions

你有两个解决方案

First Method

第一种方法

  1. use qualifier by defining a bean id init like this

    @Autowired
    @Qualifier("a")
    MyClass a;
    
    @Autowired
    @Qualifier("b")
    MyClass b;
    
  1. 通过像这样定义 bean id init 来使用限定符

    @Autowired
    @Qualifier("a")
    MyClass a;
    
    @Autowired
    @Qualifier("b")
    MyClass b;
    

Second Method

第二种方法

use JSR250 api(its a jar file you can put into your class path

使用 JSR250 api(它是一个 jar 文件,您可以将其放入类路径中

Then do autowriring like below

然后像下面这样进行自动编写

@Resource("a")
MyClass a

@Resource("b")
MyClass a