处理一个Spring bean /接口的几个实现

说我需要依赖Spring bean的几个实现 。 我有一个AccountService接口和两个实现: DefaultAccountServiceImplSpecializedAccountServiceImpl

  1. 这怎么可能(注入一个或另一个实现)在Spring?

  2. 以下注射使用哪种实施方案?

     @Autowired private AccountService accountService; 

广告。 1:您可以使用@Qualifier注释或使用@Resource自动assembly而不是@Autowired ,默认为字段名称而不是类型。

广告。 2:它将在运行时失败,说两个bean正在实现此接口。 如果您的某个bean另外使用@Primary注释 ,则在按类型自动@Primary时将首选。

 @Autowired @Qualifier("impl1") BaseInterface impl1; @Autowired @Qualifier("impl2") BaseInterface impl2; @Component(value="impl1") public class Implementation1 implements BaseInterface { } @Component(value = "impl2") public class Implementation2 implements BaseInterface { } For full code: https://github.com/rsingla/springautowire/