一.声明式事务配置:
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED" read-only="false"/>
- <tx:method name="del*" propagation="REQUIRED" read-only="false"/>
- <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
- <tx:method name="mod*" propagation="REQUIRED" read-only="false" />
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="serviceMethods" expression="execution(public * com.lexing.platform.service.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
- </aop:config>
二.声明式事务失效,原因
根本原因:由子容器扫描装配了@Service 注解的实例。
spring的context是父子容器,由ServletContextListener 加载spring配置文件产生的是父容器,springMVC加载配置文件产生的是子容器,子容器对Controller进行扫描装配时装配了@Service注解的实例 (@Controller 实例依赖@Service实例),而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。
三.解决办法
1.spring配置文件applicationContext中:
- <!-- 不扫描带有@Controller注解的类 ,让 springMVC 子容器加载。
- <context:component-scan base-package="com.lexing.platform">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
2.springMVC配置文件 servlet-context.xml中
- <!-- 将 带有 @Service注解的类,交由spring 父容器实例化,[ @Service实例依赖@Repository实例,故spring父容器也会装配<span style="font-family: Arial, Helvetica, sans-serif;">@Repository 实例</span><span style="font-family: Arial, Helvetica, sans-serif;">]</span>
- <context:component-scan base-package="com.lexing.platform">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
- </context:component-scan>
- 本文来源于:http://blog.csdn.net/z69183787/article/details/37819627
相关推荐
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
基于Spring+SpringMVC+Mybatis架构的博客系统:博客管理、图表数据、日志分析、访问记录、图库管理、资源管理、友链通知等。良好的页面预加载,无限滚动加载,文章置顶,博主推荐等。提供 用户端+管理端 的整套系统...
Spring+SpringMVC+Hibernate+maven项目,适用初学者学习。
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis).pdfSSM框架——详细整合教程(Spring+SpringMVC+MyBatis).pdf
基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场...
使用mybatis+spring+springMVC+maven+dubbo搭建的一个消费者与提供者的项目
酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统源码(spring+springmvc+mybatis) 酒店管理系统...
Spring+SpringMVC+Mybatis框架整合例子——亲测可用.zip
基于Spring + SpringMvc + MyBatis搭建的学生信息管理系统 基于Spring + SpringMvc + MyBatis搭建的学生信息管理系统 基于Spring + SpringMvc + MyBatis搭建的学生信息管理系统 基于Spring + SpringMvc + MyBatis...
Spring+SpringMvc+Mybatis学生管理系统,含登录,数据库。
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBat.
Spring+SpringMVC+Mybatis+Velocity+Maven整合
Spring+SpringMVC+Mybatis+Maven+bootstrap+ajax+jQuery整合开发简单的员工后台管理系统,免责声明,朋友分享,切勿商用
Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA
Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...
spring+springMVC+Hibernate整合
spring+springmvc+mybatis搭建的一个酒店管理系统附带mysql数据库 spring+springmvc+mybatis搭建的一个酒店管理系统附带mysql数据库
spring+springmvc+mybatis 适合练手,但是jdk版本不要太高,不然运行容易出现版本的问题