問題:Spring測試服務層能夠回滾事務,而控制器的事務卻無法回滾!
spring配置文件:
<!-- 開啓註解掃描,希望處理Service和dao,controller不需要Spring框架處理-->
<context:component-scan base-package="com.book">
<!--配置哪些註解不掃描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
springmvc配置文件:
<!--開啓註解掃描-->
<!--SpringMVC只是控制網站跳轉邏輯 -->
<context:component-scan base-package="com.book">
<!-- 只掃描controller的註解,不掃描其他-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
此時我測試Service層的事務,能夠正常回滾!,但是我打開控制器,測試卻無法回滾事務,原因時我沒有配置一下:
在Springmvc的配置文件加入以下配置:
<!--開啓註解掃描-->
<!--SpringMVC只是控制網站跳轉邏輯 -->
<context:component-scan base-package="com.book">
<!-- 只掃描controller的註解,不掃描其他-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!-- 過濾掉業務層,以及持久層,否則控制器事務無法生效-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
此時我控制器事務測試能夠正常回滾了!
原因:
springmvc配置文件中沒有過濾掉業務層,Spring容器中會有兩套@Service bean