No unique bean of Type

Source: Internet
Author: User
Beancreationexception: no unique bean of type I have defined a base class interface basedao. Below are some update \ save methods;

Then I use a basedaoimpl to implement this interface; well, now I have two Dao interfaces, one Adao extends basedao, and the other two Dao implementations: adaoimpl extends basedaoimpl implements Adao; bdaoimpl extends basedaoimpl implements bdao;

@ Repository is added to both implementations. The result is a startup error:

No unique bean of Type [COM. A. B. basedao] is defined: Expected single matching bean but found 2: [adaoimpl, bdaoimpl]


The cause of this exception is that I used the annotation @ autowird. This annotation is matched by type search and found two consistent dependency classes, for the above configuration, we found two beans of basedao: adaoimpl and bdaoimpl.


One solution for this class with multiple instances of the same type is to continue to use autowired, but use @ qualifier to specify the name of the bean, for example:

Java code
  1. @ Autowired
  2. Public void setadao (@ qualifier ("adaoimpl") Adao ){
  3. This. Adao = Adao;
  4. }


Another solution is to use the @ resource annotation. Its function is similar to @ autowired. However, you can specify the bean name or bean type to inject related beans. By default, the bean is injected by name, it is much more flexible than autowired, such:
Java code
  1. @ Resource
  2. Private Adao adaoimpl;

No unique bean of Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.