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
- @ Autowired
- Public void setadao (@ qualifier ("adaoimpl") Adao ){
- This. Adao = Adao;
- }
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
- @ Resource
- Private Adao adaoimpl;
No unique bean of Type