问题描述:

I am trying to create spring project in intellij. I have following interface:

DAO

 public interface DAO<T> {

public int create(T object);

public T read(int id);

public int update(int id, T object);

public int delete(int id);

}

I have two other classes, Employee and Customer that implements this interface with @Repository annotation on them.

When I try to @Autowire the DAO interface in my controller class, IntelliJ shows compile time error "could not autowire, multiple bean of DAO type found".

What am I doing wrong here?

网友答案:

When you set the @Autowired on property it will use the autowire byType to resolve the collaborating bean. So in the case as you've described it will produce a conflict of more than one qualifying bean.

To resolve this, you should use @Qualifier annotation, and add name to your @Repostiory annotation, something like

class YourController
{
      @Qualifier("customer")
      @Autowired    
      private Dao customerRepository;
}


@Repository("customer")
class Customer implements Dao{}

@Repository("employee")
class Employee implements Dao{}
相关阅读:
Top