spring+springMVC,聲明式事務失效,原因以及解決辦法


一.聲明式事務配置:

    <context:component-scan base-package="com.ada.wuliu"/>
    <context:component-scan base-package="com.wechat"/>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="updateOrderPayBackNotfiy" propagation="REQUIRED"
                isolation="SERIALIZABLE" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

二.聲明式事務失效,原因

根本原因:由子容器掃描裝配了@Service 注解的實例。

spring的context是父子容器,由ServletContextListener 加載spring配置文件產生的是父容器,springMVC加載配置文件產生的是子容器,子容器對Controller進行掃描裝配時裝配了@Service注解的實例 (@Controller 實例依賴@Service實例),而該實例理應由父容器進行初始化以保證事務的增強處理,所以此時得到的將是原樣的Service沒有經過事務加強處理,故而沒有事務處理能力。

三.解決辦法

1.修改spring配置文件:

<!-- 不掃描帶有@Controller注解的類 ,讓 springMVC 子容器加載。 -->
 <context:component-scan base-package="com.ada.wuliu">     
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>     
</context:component-scan>  

 


免責聲明!

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



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