Description:
Field userService in com.yh.controller.UserController required a single bean, but 2 were found:
- serviceUserServiceImpl: defined in file [C:\Users\wxt\Desktop\dubbo\consumer-service1\target\classes\com\yh\service\ServiceUserServiceImpl.class]
- userServiceImpl: defined in file [C:\Users\wxt\Desktop\dubbo\provider-service\target\classes\com\yh\service\impl\UserServiceImpl.class]
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
2023-10-02 21:36:32.969 INFO 54384 --- [bboShutdownHook] o.apache.dubbo.config.DubboShutdownHook : [DUBBO] Run shutdown hook now., dubbo version: 2.7.3, current host: 192.168.43.212
2023-10-02 21:36:32.970 INFO 54384 --- [bboShutdownHook] o.a.d.r.support.AbstractRegistryFactory : [DUBBO] Close all registries [], dubbo version: 2.7.3, current host: 192.168.43.212
Process finished with exit code 1
因为sringboot通过@Autowired注入接口的实现类时发现有多个,也就是有多个类继承了这个接口,spring容器不知道使用哪一个。
1 我自己出现该问题的场景描述:
我在实现dubbo远程调用时出出现的错误;
接口1继承了接口2,然后又在接口1的实现类中使用@Reference将接口2注入。
2 我的解决办法:
接口1不去继承接口2;
在接口1中直接定义方法,在接口1的实现类中使用@Reference将接口2注入。即:
public interface ServiceUserService{
List findList();
String msg();
}
@Service
public class ServiceUserServiceImpl implements ServiceUserService {
@Reference
private UserService userService;
public List findList() {
return userService.findList();
}
public String msg() {
return "消费者1业务层处理业务:consumer-service1"+userService.msg();
}
}
另外,我还看到了其他博主的解决方法
第一种、加@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{
}