SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法

系统:WIN8.1

数据库:Oracle 11GR2

开发工具:MyEclipse 8.6

框架:Spring3.2.9、SpringMVC3.2.9、MyBatis3.2.8


SpringMVC 的 springmvc.xml文件中 配置扫描包,不要包含 service的注解,Spring 的 配置文件配置包扫描时,不要包含controller的注解,如下所示:

Spring MVC的配置文件:

<context:component-scan base-package="包路径">

  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>

</context:component-scan> 

Spring 的配置文件:

1 <context:component-scan base-package="包路径">           
2 
3  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
4 
5 </context:component-scan>

Spring MVC启动时的配置文件,包含组件扫描、url映射以及设置freemarker参数,让spring不扫描带有@Service注解的类。

Why?

因为 springmvc.xml 与 applicationContext.xml 不是同时加载,如果不进行这样的设置,那么 Spring就会将所有带@Service注解的类都扫描到容器中,等到加载 applicationContext.xml的时候,会因为容器已经存在Service类,使得cglib将不对Service进行代理,直接导致的结果就是在 applicationContext 中的事务配置不起作用,发生异常时,无法对数据进行回滚。

原文地址:https://www.cnblogs.com/morlin/p/4383599.html