調用TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()時遇到的一些問題


之前在其他地方寫的,一直要求手機驗證,之前能跳過,麻煩點就麻煩點了.今天編輯文章的時候直接不能改了.無奈~~~~~~~~~~~

如果手動調用 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();會報如下錯誤

org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionStatus in scope

debug后發現他沒有進代理方法,在我這出現這種問題是因為我在dispatcher-servlet.xml里寫了`<context:component-scan base-package="com.xxx" />`而在applicationContext.xml里沒有注解掃描的配置.所以我懷疑就是因為沒正確加載對象導致的問題.

然后我把注解掃描分成兩個部分

    <context:component-scan base-package="com.xxx"  >
        <context:exclude-filter expression=".+Controller" type="regex"/>
    </context:component-scan>
    <context:component-scan base-package="com.xxx.controller" />

 

這時候報下面的錯

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxx' must be of type [com.Xxx], but was actually of type [com.sun.proxy.$Proxy26]

解決方法:  

<aop:aspectj-autoproxy  proxy-target-class="true"/> 

在網上找了一段相關說明,就是強制使用cglib代理時就把proxy-target-class設為true.

通過aop命名空間的<aop:aspectj-autoproxy />聲明自動為spring容器中那些配置@aspectJ切面的bean創建代理,織入切面。當然,spring在內部依舊采用AnnotationAwareAspectJAutoProxyCreator進行自動代理的創建工作,但具體實現的細節已經被<aop:aspectj-autoproxy />隱藏起來了 
<aop:aspectj-autoproxy />有一個proxy-target-class屬性,默認為false,表示使用jdk動態代理織入增強,當配為<aop:aspectj-autoproxy  poxy-target-class="true"/>時,表示使用CGLib動態代理技術織入增強。不過即使proxy-target-class設置為false,如果目標類沒有聲明接口,則spring將自動使用CGLib動態代理。 

這樣修改之后就能手動調用回滾了.

 參考

http://kld208.iteye.com/blog/1632935

 


免責聲明!

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



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