Error creating bean with name '***ManagementController': Unsatisfied dependency expressed through field '***Service';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'com.***.***.service.***Service'
available: expected single matching bean but found 2: ***ServiceImpl,***ServiceImpl
基础的SSM框架项目。
实现类A
@Service
public class AxxxServiceImpl implements AxxxxService {
}
实现类B
@Service
public class BxxxServiceImpl implements BxxxService {
}
controller中注入
@Controller
@RequestMapping("1")
public class XxxxController {
@Autowired
//@Qualifier(value = "BxxxServiceImpl")
private BxxxService bxxxService;
@Autowired
//@Qualifier(value = "AxxxServiceImpl")
private AxxxService axxxService;
@RequestMapping("/1")
@ResponseBody
private Map<String,Object> getXxxxInfo(){
// 略去
}
然后启动服务,访问后报错,信息如下:
Error creating bean with name '***ManagementController': Unsatisfied dependency expressed through field '***Service';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'com.***.***.service.***Service'
available: expected single matching bean but found 2: ***ServiceImpl,***ServiceImpl
当一个接口实现,由两个实现类时,只使用@Autowired注解,会报错。
原因是存在两个实例Aservice,Bservice,系统不知道注入哪个一个实例。
@Controller
@RequestMapping("1")
public class XxxxController {
@Autowired
@Qualifier(value = "BxxxServiceImpl")
private BxxxService bxxxService;
@Autowired
@Qualifier(value = "AxxxServiceImpl")
private AxxxService axxxService;
@RequestMapping("/1")
@ResponseBody
private Map<String,Object> getXxxxInfo(){
// 略去
}
@Controller
@RequestMapping("1")
public class XxxxController {
@Resource(name="BxxxServiceImpl")
private BxxxService bxxxService;
@Resource(name="AxxxServiceImpl")
private AxxxService axxxService;
@RequestMapping("/1")
@ResponseBody
private Map<String,Object> getXxxxInfo(){
// 略去
}
网上还有说需要在实现类中加入注解@Component(value="XxxxServiceImpl ")才能实现,就我的解决过程中,以上步骤为止就已经解决问题了。
实现类A
@Component(value="AxxxServiceImpl ")
public class AxxxServiceImpl implements AxxxxService {
}
实现类B
@Component(value="BxxxServiceImpl ")
public class BxxxServiceImpl implements BxxxService {
}
我立志做一名把细节都说清楚的博主,欢迎关注 ~
原创不易,有帮助还请鼓励个【赞】哦,谢谢无敌可爱帅气又迷人的小哥哥、小姐姐,爱你哦 ❥(^_-)~
资料参考链接:
@Autowired注解与@Qualifier注解搭配使用.