我已经尝试了所有我能想到的方法来修复这个错误,但是 spring 似乎无法区分基本的holdService和它的子activationHoldService。
@Component("ActivationHoldService")
public class ActivationHoldServiceImpl extends HoldServiceImpl {
@Component("BaseHoldService")
public class HoldServiceImpl implements HoldService {
@Component("com.erac.vos.webservice.hold.HoldWebServiceValidator")
public class HoldWebServiceValidator extends BaseValidator {
private static final Log LOG = LogFactory.getLog(
com.erac.vos.webservice.hold.HoldWebServiceValidator.class);
@Autowired
@Qualifier("BaseHoldService")
private HoldService holdService;
我正在尝试自动连接 HoldService,但我不断收到错误消息
could not autowire field: private com.erac.vos.service.vehicle.hold.HoldService com.erac.vos.webservice.vehicle.hold.HoldWebServiceValidator.holdService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.erac.vos.service.vehicle.hold.HoldService] is defined: expected single matching bean but found 2: [ActivationHoldService, BaseHoldService]
我尝试将其更改为
@Resource(name="BaseHoldService",type=com.erac.vos.service.vehicle.hold.HoldServiceImpl.class)
包括排除限定符,甚至将 baseHoldService 切换为不同的名称,以及我能想到的这些事情的任何组合,以使其正常工作,但仍然找不到它。最奇怪的部分是在不同的类中有几乎相同的代码行,但运行得很好
@Resource(name="BaseHoldService",type=com.erac.vos.service.vehicle.hold.HoldServiceImpl.class)
private HoldService defaultHoldService;
有什么想法吗?
最佳答案
spring有一个Primary注解可以帮助你“轻松”地解决问题,但我不认为这就是你要找的。
据我所知,限定符注释应该可以完成这项工作,因此您必须深入挖掘。
DefaultListableBeanFactory 中有一个名为确定PrimaryCandidate 的方法,它负责在可能的情况下解决重复项,这就是抛出异常的地方。
我会在这个方法内部进行调试,并检查为什么您的限定符未正确解析。这可能是一个简单的拼写错误,因此匹配没有按照您的预期进行,或者是一些不那么琐碎的事情,但这应该是挖掘的正确位置。
关于java - 未定义类型的唯一 bean [blah] : expected single matching bean but found 2 [moreBlah],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22466239/