1、Spring中的applicationContext.xml配置错误导致的异常

  异常信息:

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ssm.service.BTestService.getPhoneKey
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:223)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    at com.sun.proxy.$Proxy17.getPhoneKey(Unknown Source)

  如果是service接口调用出现此错误信息,注意这里不是myabtis接口mapper.java调用出现了此错误

  导致异常的错误配置如下:

  注意不要配置成  com.ssm.dao.* 这种配置方式扫描的是dao下的子包,最好配置为精准扫描包

  

  为什么 说这里被mybatis重复扫描了呢?如图所示这里的 service在debug下查看是org.apache.ibatis.binding.MapperProxy@******

  

  如果配置扫描是这种com.ssm.dao.*,会报如下错误

  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ssm.dao.AppUserPhonekeyMapper         com.ssm.service.BTestServiceImpl.appUserPhonekeyMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type   [com.ssm.dao.AppUserPhonekeyMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295)

 2、以为是Spring中bean命名导致的错误

  异常信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beanTestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ssm.service.TBestService com.ssm.controller.BeanTestController.tBestService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.ssm.service.TBestService] is defined: expected single matching bean but found 2: TBestServiceImpl,TBestService

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ssm.service.TBestService com.ssm.controller.BeanTestController.tBestService; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.ssm.service.TBestService] is defined: expected single matching bean but found 2: TBestServiceImpl,TBestService
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531)

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.ssm.service.TBestService] is defined: expected single matching bean but found 2: TBestServiceImpl,TBestService

  配置如下:

    这里的spring核心配置文件中的mybatis扫描配置还是最大范围的,包含了service接口,这里controller中配置如下

    注意:上面的异常是不能找到注入的名称,命名规则如下(myabtis应该是这样的!,spring中@Autowired注入名称正常是任意的命名),如果controller写成了   

         private TBestService TBestService  这种写法启动不报错,但是这时候的TBestService 在DEBUG模式下查看还是第一种里面的MapperProxy ,运行时还是会报第一种错

3、Spring @Autowired注入名称正常是任意的命名,测试如下:

  spring自动对应找到了,service接口的实现类

  有人可能怀疑是 注解的问题@Autowired 和@Resource不一样,测试过,是一样的!!具体这两个注解的区别大家可以参考 :

    https://www.cnblogs.com/smileLuckBoy/p/5801678.html

4、补充一下原来遇到的坑:Spring中的事务失效,事务不回滚原因

  正常情况就是注解扫描的问题,

  原来失效情况是这样的:

  spring中的applicationContext.xml中的扫描 

 <context:component-scan base-package="com.ssm.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> 这里扫描了service接口以及实现,但是springmvc扫描又把service接口和实现扫描了一遍,导致了springmvc接管了service接口,正常的访问和其他操作都没问题,当需要事务回滚就不好用了。  
总结: 在这里建议大家在spring xml文件配置中需要注意扫描的范围. 
   1、spring负责扫描全局范围的注解   
   2、mybatis注解扫描 dao的包    
   3、springmvc需要扫描 controller 
  千万不要扩大范围,很容易带来一些费解的错误.

Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因的更多相关文章

  1. Spring @Transaction配置演示样例及发生不回滚原因深度剖析

    背景 近期在公司做的一个项目,用的是SpringMVC框架,数据库用的是MySql,刚開始并没有增加事务,后因业务须要必须事务处理. 问题的产生和解决 使用事务,直接问百度,我选择的是注解的方式. 在 ...

  2. spring data jpa使用@Transactional注解开启事务后失败不回滚

    如题,在数据库批量操作方法上使用@Transactional注解,其中一条数据抛出异常了,却死活不回滚. 批量操作方法是公有的,spring也是默认支持事务的,排除代码层面问题,那么就看看数据库是否支 ...

  3. Spring事务不回滚原因分析

    Synchronized用于线程间的数据共享,而ThreadLocal则用于线程间的数据隔离. 在我完成一个项目的时候,遇到了一个Spring事务不回滚的问题,通过aspectJ和@Transacti ...

  4. @Transactional注解不回滚原因详解

    最近试了试spring的回滚功能,根据网上的教程配置怎么都不好使,遂寻找答案, 网上的答案都是这么讲的: 1. 检查你方法是不是public的. 2. 你的异常类型是不是unchecked异常.如果我 ...

  5. Spring transaction事务之roll back回滚

    转载自:http://blog.csdn.net/lovejavaydj/article/details/7635848 试验方法: 写一个单元测试,调用一个service层方法(发生对数据库进行写操 ...

  6. spring事务不会进行回滚的情况

    if(userSave){ try { userDao.save(user); userCapabilityQuotaDao.save(capabilityQuota); } catch (Excep ...

  7. Spring transaction事务 roll back各种回滚

    Spring的AOP事务管理默认是针对unchecked exception回滚. 也就是默认对RuntimeException()异常极其子类进行事务回滚. Exception作为基类,下面还分ch ...

  8. Spring+Junit,测试事务时,一直回滚

    研究了好长时间,都不知道原因,也不能解决. 控制台又没有报异常,但是就是一直回滚.注释掉spring声明式配置的代码,就能正确的更新数据. 从网上看了,别人的文章 http://blog.csdn.n ...

  9. mysql事务管理及spring声明式事务中主动异常抛出使数据库回滚

    mysql的引擎常用的有两个,一个MyISAM,另一个是InnoDB,mysql默认的为MyISAM,而InnoDB才是支持事务的.所以一般需要修改下,如何修改就不说了. 事务需要依赖数据库,好久没使 ...

随机推荐

  1. spark配置参数

    ~/.bashrc export SPARK_HOME=/usr/local/spark export PATH=$PATH:$SPARK_HOME/bin /usr/local/spark/conf ...

  2. finalize方法的使用

    finalize()是在java.lang.Object里定义的,也就是说每一个对象都有这么个方法.这个方法在gc启动,该对象被回收的时候被调用.其实gc可以回收大部分的对象(凡是new出来的对象,g ...

  3. codewars 题目笔记

    原题: Description: Bob is preparing to pass IQ test. The most frequent task in this test is to find ou ...

  4. 在码云(gitee)上展开程序类课程教学

    码云主要提供了源代码管理(Git/SVN)功能,最近又推出了高校版让普通老师也能利用起来以供教学使用. 学生与老师不仅能利用其管理代码,更重要的是我们的程序教学能通过对git的使用来引入业界流行的软件 ...

  5. Must Know Tips/Tricks in Deep Neural Networks

    Must Know Tips/Tricks in Deep Neural Networks (by Xiu-Shen Wei)   Deep Neural Networks, especially C ...

  6. 吴恩达机器学习笔记44-核函数(Kernels)

    回顾我们之前讨论过可以使用高级数的多项式模型来解决无法用直线进行分隔的分类问题: 为了获得上图所示的判定边界,我们的模型可能是

  7. Python面向对象5:类的常用魔术方法

    魔术方法就是不需要人为调用的方法,基本是在特定的时刻自动触发- 魔术方法的统一的特征,方法名被前后各两个下滑线包裹 - 操作类 - `__init__`: 构造函数 - `__new__`: 对象实例 ...

  8. Docker学习笔记-磁盘挂载运行.netcore

    前言: 环境:centos7.5 64 位 正文: 首先我们在宿主机上安装 .NET Core SDK sudo rpm --import https://packages.microsoft.com ...

  9. Maven 项目打包出现错误 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin

    今天碰到一个奇怪的问题,就是我在eclipse中使用maven命令:clean package 命令打完包之后,通过FlashFXP将jar包上传到Linux服务器后,由于其他原因,我想要修改下程序重 ...

  10. Kali学习笔记10:端口扫描详解(下)

    上一篇先是介绍了UDP的端口扫描,又谈了TCP的不完全连接端口扫描 https://www.cnblogs.com/xuyiqing/p/9389276.html 接下来我们看看TCP的全连接端口扫描 ...