相关文章推荐
要出家的灌汤包  ·  Java怎么将ByteArrayInputS ...·  2 月前    · 
留胡子的西瓜  ·  jacksonxmlproperty - ...·  8 月前    · 
乐观的蚂蚁  ·  Excel·VBA二维数组组合函数的应用实例 ...·  1 年前    · 
博学的双杠  ·  Python实现小功能--替换文件中某个字符 ...·  1 年前    · 
爱健身的芒果  ·  Python 字符串翻转 | 菜鸟教程·  1 年前    · 
小百科  ›  【报错解决】expected single matching bean but found 2_我梦Leo的博客
bean
严肃的吐司
1 年前
  • 报错信息展示:
  • 项目背景:
  • 报错还原:
  • 原因分析:
  • 解决方案:
    • 方案一:使用@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框架项目。

    报错还原:

    @Service
    public class AxxxServiceImpl implements AxxxxService {
    
    @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,系统不知道注入哪个一个实例。

    解决方案:

    方案一:使用@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<String,Object> 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<String,Object> getXxxxInfo(){
        	// 略去
    

    补充说明:

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

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

    我立志做一名把细节都说清楚的博主,欢迎关注🎉 ~
    原创不易,有帮助还请鼓励个【赞】哦,谢谢无敌可爱帅气又迷人的小哥哥、小姐姐,爱你哦 ❥(^_-)~

    拓展:@AutoWired、@Resource、@Qualifier理解

    资料参考链接:
    @Autowired注解与@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.***.***.servic @Slf4j @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:test-application-context.xml") public class TcActCon...
    我想使用resultMap 一对多查询 可是提示 Expected one result (or null) to be returned by selectOne(), but found:18 下面展示一些 mapper.xml。 SELECT st.title as titlest ,st.info_text as info_text , sbt.title as titlesbt,sbt.icon as icon, sbx.text as text FROM `services_title` AS st @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {} 3、entity类 public class User { @Tabl
    available: expected single matching bean but found 2: smsLogServiceImpl,ISmsLogService 如上图,是在启动的时候出现的问题,从字意上可以看出,是说在注入bean的时候,出现了两个,spring不知道应该用哪个为好,这里说下可能出现到的原因: 1、譬如service层和mapper层没有引入对应的注解(@Service/@Mapper)引起的。 2、一个接口被两个实现类实现了,当使用到@Autowired注解引入的时候.
    今天在使用springboot做多数据源学习的时候,在测试不同数据插入不同数据库时,出现了如题“available: expected single matching bean but found 2”的错误,错误大概的意思是指配置文件中 事务匹配到了俩个事务管理,系统不知道具体指向谁,错误如下 随后,我就此错误上网查询,在使用@primary注解注解其中一个数据源事务管理后,该数据源的事务功...
    expected single matching bean but found 2 No qualifying bean of type [java.lang.String] is defined: expected single matching bean but found 2: 这些错误都是由于Spring容器自动装配无法匹配合适的bean引起的,值得说明的是,在使用配置文件方式配置bean时,在配置文件中配置bean时,需要特别注意那个 name 属性,而不是 id 属性,
    2.查询多条数据,但是前台限制只能查询一条 (1) 检查数据sql语句写对没有 (2) 查看mybaties的返回值(resultType或resultMap),可以返回list,map等数据 (3) dao层接口要与mybaties中返回值和接收值都要保持一致 service层 User login(User user); serviceImpl层 @Override public User login(User user) { return userMapper.login(user);     在运行时出现了 [: =: unary operator expected 的错误,就一直找不到原因,尝试了删除等号两侧的空格和括号里的空格都不管用,最后baidu了一下,才找到原因。把语句改成这样就不会出错了. if [[ $STATUS = OK ]]; echo OK     究其原因,是因为如果变量STATU
    解决 VSCode 编辑 vue 项目报错 Expected indentation of 2 spaces but found 4解决 VSCode 编辑 vue 项目报错 Expected indentation of 2 spaces but found 4问题问题分析解决办法一解决办法二 解决 VSCode 编辑 vue 项目报错 Expected indentation of 2 spaces but found 4 系统:Win10 编辑器:VSCode 使用 VSCode 在写 Vue 项目的时候,出现报错 Expected indentation of 2 spaces
    springboot项目,之前有mysql数据源,现在又新增了clickhouse数据源,于是新增了一个clickhouseDatasource的配置bean,结果报错,如下: nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'get...
    报错expected single matching bean but found 2: requestMappingHandlerMapping。。。 附链接:https://www.yepk.cn/archives/mvc-error-bean-but-found2 没找到报错的原因,按反正是照博主的方法解决了 【报错解决】ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO) 如果288: 如果不知道root密码怎么办, 【报错解决】expected single matching bean but found 2 NJU_罗成: 按照你的,@Qualifier(value = "userDaoImpl") 可是还是报错:No qualifying bean of type 'com.lc.AutoWired.Dao.UserDao' available: expected single matching bean but found 2: redisDaoImpl,userDaoImpl 【问题解决】vue中,向一个List内添加单个对象; 向一个List内添加另一个List(拼接两个List)。 我梦Leo: 哪里有问题,还请您帮忙指出一下,以便我改正,避免给其他小伙伴带来误导表情包 mysql启动失败The server quit without updating PID file (/usr/xxx/mysql/mysql-8.0/data/xxx.pid) 【问题解决】return 返回URL字符串,不跳转。 【问题解决】SpringBoot单元测试没有绿色运行符号
 
推荐文章
要出家的灌汤包  ·  Java怎么将ByteArrayInputStream 转成FileInputStream_mob64ca12e41d46的技术博客_51CTO博客
2 月前
留胡子的西瓜  ·  jacksonxmlproperty - CSDN文库
8 月前
乐观的蚂蚁  ·  Excel·VBA二维数组组合函数的应用实例-CSDN博客
1 年前
博学的双杠  ·  Python实现小功能--替换文件中某个字符串_python替换文件中的字符串-CSDN博客
1 年前
爱健身的芒果  ·  Python 字符串翻转 | 菜鸟教程
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
小百科 - 百科知识指南
© 2024 ~ 沪ICP备11025650号