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. 中间件 activeMQ Jms Java Demo

    一.什么是ActiveMQ 百度解释: ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provi ...

  2. js实用方法记录-指不定哪天就会用到的js方法

    js实用方法记录-指不定哪天就会用到的js方法 常用或者不常用都有 判断是否在微信浏览器中 测试代码:isWeiXin()==false /** * 是否在微信中 */ function isWeix ...

  3. Java学到什么程度才能叫精通?

      ​ 把下面这些内容掌握以后,你就可以自诩精通Java后端了. 1 计算机基础 这部分内容是计算机相关专业同学的课程,但是非科班的小伙伴(譬如在下)就需要花时间恶补了. 特别 是计算机网络,操作系统 ...

  4. Javascript高级编程学习笔记(77)—— 表单(5)过滤输入

    在日常实践中,我们常常会需要用户按照某种规则输入数据 但是文本框在默认情况下缺少验证数据的手段,因此需要使用JS来完成此类过滤输入的操作 通过事件和DOM的结合手段就能够将普通的文本框转换为功能型控件 ...

  5. JavaScript 基础排序的实现(一)

    作为一个有追求的前端,忙里偷闲(闲得发慌)地复习了一下基础的排序算法,以此文留念. 本篇主要记录O(n²)复杂度的基础算法O(nlogn)的算法将在下次有空(闲得发慌)时更新 在记录时发现Es6语法中 ...

  6. Ubuntu18.04版本设置root账户

    Linux系统下文件的权限十分重要,大多数操作都需要一定的权限才可以操作,Ubuntu18.04默认安装是没有设置root账户的,因此想要获得root账户登录可以使用以下步骤:   1.首先获得临时的 ...

  7. 常见的anaconda的操作

    以前对anaconda的理解比较少,以为它就是一个比较大的python库,现在发现它原来不止是这样,它还有很多其他用途. Anaconda指的是一个开源的Python发行版本,其包含了conda.Py ...

  8. 解决Database returned an invalid datetime value. Are time zone definitions for your database installed?

    设定博客文章按照时间分层筛选出现问题 ret=Article.objects.filter(user=user).annotate(month=TruncMonth("create_time ...

  9. VueJs(14)---理解Vuex

    理解Vuex 一.Vuex 是什么? 首先我们来分析一种实际开发中用vue.js的场景,你有n个组件,当你改变一个组件数据的时候需要同时改变其它n个组件的数据,那么我想你可能会对 vue 组件之间的通 ...

  10. C#4并行计算

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...