spring整合mybatis
使用声明式注解的方式为方法添加上事务,但是再测试的时候总是不生效,查找了许多原因。最后发现是配置文件的问题。
springMVC的配置文件(应该只去扫描控制器组件,当时我扫描了整个包)
<context:component-scan base-package="com.xxx.controller"></context:component-scan> /
而spring的配置文件(扫描除了控制器以外的其它组件)
<!--扫描组件(除控制层)-->
<context:component-scan base-package="com.xxx.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
原理就是父子容器,子容器扫描范围过大,把未加事务配置的 service 扫描进来,每次去拿对象时,先回去子容器里面找,然而子容器是原始对象,导致事务失效。
解决方法就是各扫描各的,或者使用同一容器 ,(springboot就是)