Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Autowiring & No unique bean

Tags:

spring

I have 2 classes (B,C) extends class 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]

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

Thanks in advance.

like image 588
mada Avatar asked Apr 23 '10 15:04

mada


People also ask

Why is @autowired not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

Why Autowired is getting null?

If the application starts and your field appears to be null it is generally due to one of the following issues: Using @Autowired on a static field. Omitted @Autowired on a field. Instance of bean not visible to Spring.

Should I use inject or Autowired?

Since Spring is the most popular DI and IOC container for Java application, @Autowired is more common and @Inject is lesser-known, but from a portability point of view, it's better to use @Inject. Spring 3.0 supports JSR-330 annotation, so if you are using Spring 3.0 or higher release, prefer @Inject over @Autowired.


2 Answers

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

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

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.

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

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

like image 117
Espen Avatar answered Sep 24 '22 14:09

Espen


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

@Autowired
private A beanA;

But you have 2 beans that conform to this.

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

@Resource("b")
private A beanA;

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

like image 34
Bozho Avatar answered Sep 24 '22 14:09

Bozho