码上敲享录 > SpringBoot常见问题详解 > Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or us

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or us

上一章章节目录下一章 2020-01-08已有27347人阅读 评论(0)

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


解决方法:

springboot启动报错如下:

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Disconnected from the target VM, address: '127.0.0.1:54176', transport: 'socket'

Process finished with exit code 1

因为sringboot通过@Autowired注入接口的实现类时发现有多个,也就是有多个类继承了这个接口,spring容器不知道使用哪一个。


第一种、加@Primary表示某个实现类首选注入

@Configuration("")

public class ActivitiConfig {

   @Autowired

   ProcessEngineConfiguration processEngineConfiguration;

   //流程配置,与spring整合采用SpringProcessEngineConfiguration这个实现

   @Bean

   @Primary

   public ProcessEngineConfiguration processEngineConfiguration(DataSource dataSource,

                                                                PlatformTransactionManager transactionManager){

       return processEngineConfiguration;

   }

}


第二种、@Autowired和@Qualifier(value = "beanName")结合使用

@Configuration

public class ActivitiConfig {

   @Autowired

   @Qualifier(value = "processEngineConfigurationImpl")

   ProcessEngineConfiguration processEngineConfiguration;

}


@Service("processEngineConfigurationImpl")

public class ProcessEngineConfigurationImpl extends  ProcessEngineConfiguration{


}


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
2

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交