栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【报错】expected single matching bean but found 2

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【报错】expected single matching bean but found 2

【报错】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
  • 报错信息展示:
  • 项目背景:
  • 报错还原:
  • 原因分析:
  • 解决方案:
    • 方案一:使用@Qualifier注解来指明注入的实例。
    • 方案二:使用@Resource(name="Xxxservice")注解来指明注入的实例。
      • 补充说明:
  • 拓展:@AutoWired、@Resource、@Qualifier理解

报错信息展示:
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 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,系统不知道注入哪个一个实例。

解决方案: 方案一:使用@Qualifier注解来指明注入的实例。
@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 getXxxxInfo(){
    	// 略去
    }
方案二:使用@Resource(name=“Xxxservice”)注解来指明注入的实例。
@Controller
@RequestMapping("1")
public class XxxxController {
    @Resource(name="BxxxServiceImpl")
    private BxxxService bxxxService;

    @Resource(name="AxxxServiceImpl")
    private AxxxService axxxService;

    @RequestMapping("/1")
    @ResponseBody
    private Map getXxxxInfo(){
    	// 略去
    }
补充说明:

网上还有说需要在实现类中加入注解@Component(value="XxxxServiceImpl ")才能实现,就我的解决过程中,以上步骤为止就已经解决问题了。

实现类A

@Component(value="AxxxServiceImpl ")
public class AxxxServiceImpl implements AxxxxService {
}

实现类B

@Component(value="BxxxServiceImpl ")
public class BxxxServiceImpl implements BxxxService {
}
拓展:@AutoWired、@Resource、@Qualifier理解

资料参考链接:
@Autowired注解与@Qualifier注解搭配使用.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/314499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号