欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring+SpringMVC配置事務(wù)管理無效原因及解決辦法詳解

 更新時(shí)間:2017年12月26日 16:13:02   作者:等想出來再取  
這篇文章主要介紹了Spring+SpringMVC配置事務(wù)管理無效原因及解決辦法詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下

一般我們?cè)赟pring的配置文件application.xml中對(duì)Service層代碼配置事務(wù)管理,可以對(duì)Service的方法進(jìn)行AOP增強(qiáng)或事務(wù)處理如事務(wù)回滾,但是遇到一個(gè)問題,在Controller類中調(diào)用Service層方法,配置的事務(wù)管理會(huì)失效,查詢相關(guān)資料發(fā)現(xiàn)原因。其實(shí)Spring和SpringMVC倆個(gè)容器為父子關(guān)系,Spring為父容器,而SpringMVC為子容器。也就是說application.xml中應(yīng)該負(fù)責(zé)掃描除@Controller的注解如@Service,而SpringMVC的配置文件應(yīng)該只負(fù)責(zé)掃描@Controller,否則會(huì)產(chǎn)生重復(fù)掃描導(dǎo)致Spring容器中配置的事務(wù)失效。

因此正確的配置方式應(yīng)該為:

Spring的配置文件:application.xml

  <context:component-scan base-package="org.bc.redis" use-default-filters="true">
    <!-- 排除含@Controller注解的類 -->
    <context:exclude-filter type="annotation" expression="org.bc.redis.controller.UserController"/>
  </context:component-scan>

或者

  <!-- 指定掃描的包,避開包含@Controller注解的包 -->
  <context:component-scan base-package="org.bc.redis.service" use-default-filters="true">
  </context:component-scan>

SpringMVC的配置文件:springmvc.xml

  <!-- 只掃描含@Controller注解的包,避免重復(fù)掃描 -->
  <context:component-scan base-package="org.bc.redis.controller" use-default-filters="true">
  </context:component-scan>

最后

經(jīng)過測(cè)試,其實(shí)問題主要在于SpringMVC的配置文件掃包范圍,Spring的配置文件就算也掃了@Controller注解,但是在SpringMVC會(huì)重新掃描一次,事務(wù)管理的Service只要沒被重新掃描就不會(huì)出現(xiàn)事務(wù)失效問題。

總結(jié)

以上就是本文關(guān)于Spring+SpringMVC配置事務(wù)管理無效原因及解決辦法詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

最新評(píng)論