applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.myland.*" >
</context:component-scan>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${mysql.driverClassName}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="initialSize" value="${mysql.initialSize}"></property>
<property name="maxActive" value="${mysql.maxActive}"></property>
<property name="maxIdle" value="${mysql.maxIdle}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${mysql.timeBetweenEvictionRunsMillis}"></property>
<property name="minEvictableIdleTimeMillis" value="${mysql.minEvictableIdleTimeMillis}"></property>
</bean>
<!-- 阿里连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 驱动名称 -->
<property name="DriverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${mysql.url}" />
<!-- 数据库用户名称 -->
<property name="username" value="root" />
<!-- 数据库密码 -->
<property name="password" value="root" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 初始化大小 -->
<property name="initialSize" value="5" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="2" />
<!-- 逐出连接的检测时间间隔 -->
<property name="timeBetweenEvictionRunsMillis" value="3000" />
<!-- 最小逐出时间 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 测试有效用的SQL Query -->
<property name="validationQuery" value="SELECT 'x'" />
<!-- 连接空闲时测试是否有效 -->
<property name="testWhileIdle" value="true" />
<!-- 获取连接时测试是否有效 -->
<property name="testOnBorrow" value="false" />
<!-- 归还连接时是否测试有效 -->
<property name="testOnReturn" value="false" />
<!-- 统计监控信息 -->
<property name="filters" value="stat,wall,log4j" />
</bean>
<!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations">
<list>
<value>classpath*:com/myland/myapp/**/*Mapper.xml</value>
</list>
</property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!-- 事务处理 begin -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="baseServiceMethods" expression="execution(* com.myland.demo.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 事务处理 end -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.myland..Service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<context:component-scan base-package="com.myland.*" >
</context:component-scan>
<mvc:annotation-driven />
<!-- 必须添加否则拦截器不管作用 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="OperLogInterceptor" class="com.myland.framework.log.OperLogInterceptor"></bean>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
以上是工程中原有spring配置文件。在测试事务时,事务不能回滚。原因是applicationContext.xml是主容器在扫描类中service,发现service进行事务处理。在主容器初始化后,springmvc-servlet在进行一次扫描,这次扫描是@Controller的注解bean,但是@service的bean只是作为普通bean进行处理,不进行事务处理。所有spring容器装载service的bean只是作为普通bean使用。
确认事务无法回滚的几个问题。
1.确认mysql表类型innodb类型
2.确认配置文件配置是否正确,包括:service中路径正确
3.在配置注解扫描时,主容器只扫描@service,mvc容器值扫描@controller的注解。
主容器
<context:component-scan base-package="com.myland.*" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
mvc容器
<context:component-scan base-package="com.myland.*" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
相关推荐
Spring/SpringMVC/MyBatis整合,可事务回滚
springmvc 环境搭建 demo springmvc 环境搭建 demo springmvc 环境搭建 demo springmvc 环境搭建 demo springmvc 环境搭建 demo springmvc 环境搭建 demo springmvc 环境搭建 demo springmvc 环境搭建 demo ...
springMVC+MYbatis框架搭建与事务管理
springMVC项目搭建源码与步骤文档
使用springMVC4 搭建Restful轻服务。
web工程中,用springmvc搭建项目时所需jar包,版本是4.2.1以及log4j2的jar包
springmvc搭建初步搭建教材一些初步教材适合新人用
此文档详细讲解了如何搭建springmvc项目,springmvc好教程
springMVC框架搭建基本代码
每次需要Spring MVC的web项目测试一些东西时,都苦于手头上没有最简单的Spring MVC的web项目,现写一个SpringMVC完整搭建项目,下载即可运行,供初学者学习
Springmvc tx标签事务管理,服务器启动时加载顺序的问题对事务的影响
hibernate5.2+SpringMVC4.3框架搭建源码。
springMVC搭建详情,里面还有log4j的架包搭配。简单易懂
SpringMVC搭建,适合初学者学习
springMVC+mybatis搭建框架springMVC+mybatis搭建框架
这是一个完整的mybaits3.0+springmvc+oracle11g项目,包括sql,存储过程等,下载修改数据库连接 即可运行,包括数据增删改查,一对多查询,调用存储过程。完整的配置文件注释,绝对是入门级教材!另附有根据汉字获取...
最完整的Spring+Mybatis+SpringMVC+MySql的搭建实例
最简单的springmvc4.0框架搭建, 绝对可以运行
使用JavaConfig搭建SpringMVC实现零XML配置