简体   繁体   English

如何在具有扩展另一个接口的接口的实现上设置限定符

[英]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.

相关问题 如何模拟扩展另一个接口的接口 - How to mock an interface which extends another interface 如何创建具有扩展另一个类并实现接口的类型参数的基适配器? - How to create base adapter which has type parameter that extends another class and implements an interface? 如何注入一个接口,该接口继承自另一个在 Spring 中具有实现的接口 - How to inject an interface that inherits from another interface that has an implementation in Spring 自动装配一个扩展另一个接口的接口 - Autowire an interace which extends another interface 是否等于Java类中的实现,该类实现了扩展Iterable的接口? - Equals implementation in a java-class which implements an interface that extends Iterable? 如何实现已被参数化并扩展了另外两个接口的接口? - How to implement an interface which has been parameterized and extends two other interfaces? 如何确保接口实现扩展特定类? - How can I ensure that an interface implementation extends a particular class? 类实现扩展另一个接口的接口 - Class implements Interface that extends another Interface Javassist:创建一个使用泛型扩展另一个接口的接口 - Javassist: creating an interface that extends another interface with generics 接口扩展另一个接口但实现其方法 - Interface extends another interface but implements its methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM