前言
在spring下面假如你定义了自动扫描:

然后,service长这样:

biz继承了service:

执行时候你会发现:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name '/admin/account/Member':
Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'net.w2p.DevBase.service.account.MemberService' available:
expected single matching bean but found 2: memberService,memberBiz
好了,这个怎么会出现呢,搞不好是spring看在继承份上连父亲节点也一并初始化了,原本就有父类了。。
解决方案:
Spring: Cannot autowire a Bean which extends another Bean


推荐的是:
How to extend @Service with spring




所以,加一个primary即可:

本文探讨了在Spring框架中,当使用自动扫描时,如何解决因多重继承导致的Bean实例化冲突问题。通过示例说明了错误发生的原因,并提供了解决方案,即通过添加primary属性来指定首选Bean。
1910

被折叠的 条评论
为什么被折叠?



