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

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 
  千萬不要擴大范圍,很容易帶來一些費解的錯誤.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM