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

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)}

扫描二维码关注公众号,回复: 1702902 查看本文章

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,

  千万不要扩大范围,很容易带来一些费解的错误。


猜你喜欢

转载自www.cnblogs.com/liran123/p/9214631.html