[英]How to set Qualifier on an Implementation that has an interface which extends another interface
I have 2 Interfaces A and B which extends another Interface C. However when i use the @Qualifier("aImpl") i get a unsatisfactionary dependency from spring 我有2个接口A和B,它们扩展了另一个接口C。但是,当我使用@Qualifier(“ aImpl”)时,我从spring获得了不满意的依赖关系
The interfaces 接口
public interface EsResourceLoader { ... }
public interface CommonEsDao extends EsResourceLoader { ... }
public interface CommonModifiedEsDao extends EsResourceLoader { ... }
The first implementation 第一次实施
@Service
public class LBModifiedEsDao implements CommonModifiedEsDao { ... }
The second implementation 第二实施
@Service
public class LBDao implements CommonEsDao {
private CommonModifiedEsDao myDao;
@Autowired
public LBDao(@Qualifier("lBModifiedEsDao") CommonModifiedEsDao myDao) { ...
}
}
The controller throwing the exception 控制器抛出异常
@RestController
@RequestMapping{...}
public class IndexAdminController {
private CommonEsDao esDao;
@Autowired
public IndexAdminController(@Qualifier("lBDao")CommonEsDao esDao){ ... }
}
And here is the Exception 这是例外
Exception encountered during context initialization - cancelling refresh
attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'indexAdminController': Unsatisfied dependency
expressed through field 'lbDao';
Put your @Qualifier annotation to service and it should works. 将您的@Qualifier批注投入服务,它应该可以工作。
@Service
@Qualifier("lBModifiedEsDao")
public class LBModifiedEsDao
implements CommonModifiedEsDao { ... }
And also for second service 还有第二种服务
@Service
@Qualifier("lBDao")
public class LBDao implements CommonEsDao {
private CommonModifiedEsDao myDao;
@Autowired
public LBDao(@Qualifier("lBModifiedEsDao") CommonModifiedEsDao myDao) { ...
}
}
Now Spring should be able to always autowire it correctly. 现在,Spring应该能够始终正确地对其进行自动接线。 Also this is also nice because you can be sure with that you will have always correct implementation. 这也很好,因为您可以肯定将始终具有正确的实现。
EDIT: As I did in your code with @Qualifier
annotation can be achieved by using name in Component annotations, ie.: @Component("myComponent")
编辑:就像我在代码中使用@Qualifier
注释所做的那样,可以通过在组件注释中使用名称来实现,即: @Component("myComponent")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.