Spring无法自动装配,存在不止一个\`\`类型的bean

新手上路,请多包涵

这是我的问题:我有一个基本接口和两个实现类。

而一个Service类对基接口有依赖,代码是这样的:

 @Component
public interface BaseInterface {}


 @Component
public class ClazzImplA implements  BaseInterface{}


 @Component
public class ClazzImplB implements  BaseInterface{}


配置是这样的:

 @Configuration
public class SpringConfig {
    @Bean
    public BaseInterface clazzImplA(){
        return new ClazzImplA();
    }

    @Bean
    public BaseInterface clazzImplB(){
        return new ClazzImplB();
    }
}


服务类对基接口有依赖,将由一些业务逻辑决定自动装配哪个实现。代码是这样的:


 @Service
@SpringApplicationConfiguration(SpringConfig.class)
public class AutowiredClazz {
    @Autowired
    private BaseInterface baseInterface;

    private AutowiredClazz(BaseInterface baseInterface){
        this.baseInterface = baseInterface;
    }
}

IDEA 抛出异常:Could not autowire.There is more than one bean of BaseInterface type。

虽然可以使用@Qualifier 来解决,但是在这种情况下我无法选择依赖类。

 @Autowired
@Qualifier("clazzImplA")
private BaseInterface baseInterface;

我试图阅读 spring 文档,它提供了一个 Constructor-based dependency injection 但我仍然对这个问题感到困惑。

谁能帮我 ?

原文由 0x822a5b87 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.3k
2 个回答

Spring 混淆了您在配置类中声明的 2 个 bean,因此您可以使用 @Qualifier 注释以及 @Autowired 通过指定将连接哪个 bean 来消除混淆,应用这些修改你的配置类

@Configuration
public class SpringConfig {
    @Bean(name="clazzImplA")
    public BaseInterface clazzImplA(){
        return new ClazzImplA();
    }

    @Bean(name="clazzImplB")
    public BaseInterface clazzImplB(){
        return new ClazzImplB();
    }
}

然后在 @autowired 注释

@Service
@SpringApplicationConfiguration(SpringConfig.class)
public class AutowiredClazz {
    @Autowired
    @Qualifier("the name of the desired bean")
    private BaseInterface baseInterface;

    private AutowiredClazz(BaseInterface baseInterface){
        this.baseInterface = baseInterface;
    }
}

原文由 Mohamed Nabli 发布,翻译遵循 CC BY-SA 3.0 许可协议

仅使用spring框架是解决不了的。您提到基于某些逻辑您需要一个 BaseInterface 实例。这个用例可以使用工厂模式来解决。创建一个 Bean,它实际上是 BaseInterface 的工厂

@Component
public class BaseInterfaceFactory{

  @Autowired
  @Qualifier("clazzImplA")
  private BaseInterface baseInterfaceA;

  @Autowired
  @Qualifier("clazzImplb")
  private BaseInterface baseInterfaceB;

  public BaseInterface getInstance(parameters which will decides what type of instance you want){
    // your logic to choose Instance A or Instance B
    return baseInterfaceA or baseInterfaceB
  }

}

配置(无耻地从另一个评论中复制)

 @Configuration
public class SpringConfig {
    @Bean(name="clazzImplA")
    public BaseInterface clazzImplA(){
        return new ClazzImplA();
    }

    @Bean(name="clazzImplB")
    public BaseInterface clazzImplB(){
        return new ClazzImplB();
    }
}

服务等级

@Service
@SpringApplicationConfiguration(SpringConfig.class)
public class AutowiredClazz {
    @Autowired
    private BaseInterfaceFactory factory;

    public void someMethod(){
       BaseInterface a = factory.getInstance(some parameters);
       // do whatever with instance a
    }
}

原文由 Sangram Jadhav 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题