1. 程式人生 > >spring,springmvc,hibernate整合事務不起作用

spring,springmvc,hibernate整合事務不起作用

剛學習完這三大框架,跟著別人的部落格整合一下,就測試一下事務是否起作用,用的1/0來測試,用的@Transactional註解在service層,發現事務始終不起作用,也在網上查了一些資料,發現spring,springmvc配置檔案中掃描包衝突了,因為我的是在web.xml中先載入spring的配置檔案,然後載入springmvc的配置檔案(兩個都是掃描全部的包),看別人的部落格說後者的掃描覆蓋了前者,而springmvc沒有配置事務管理器,所以導致事務不起作用.附上配置檔案

spring的配置檔案

<!-- 開啟自動掃描的包 -->
<context:component-scan base-package="com">
<!-- 不掃描控制器 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>


springmvc配置檔案

<context:component-scan base-package="com" use-default-filters="false">
   <!-- 只掃描帶有controller註解的控制器 -->
   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
   </context:component-scan>

注意springmvc配置檔案要用 use-default-filters="false",

因為use-default-filters的值預設是true,也就是掃描全部的帶有@Controller、@Service等註解的包了,加上之後則只掃描controller.