项目使用spring+springmvc+mybatis,配置完之后发现事务没起作用,捣鼓了几乎一个白天。
废话不多说,首先发现切面几乎没起作用,只有mapper能被切到,servicce层切不到。
最后的解决方案是,springmvc和spring是两个配置文件,修改spring-mvc.xml的扫描路径,把service排除,顺便把spring.xml的扫描路径里control排除。因为重复扫描会导致spring事务失效。
做如下配置:
spring-mvc.xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<!-- 配置扫描的包 --> <context:component-scan base-package="com.eifini.cms" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
spring.xml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<context:component-scan base-package="com.eifini.cms"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>