spring 事务管理的两种配置方法
1. xml配置
2. 注解
一、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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <context:component-scan base-package="com.spring"></context:component-scan> <!-- 1,引入外部contextSchema命名空间; 2,导入外部配置文件 --> <context:property-placeholder location="classpath:./jdbc.properties" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 创建spring NamedParameter JdbcTemplate --> <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate"> <constructor-arg name="dataSource" ref="dataSource"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事务切面 --> <aop:config> <aop:pointcut expression="execution(* com.spring.service.*.*(..))" id="serviceMethod"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/> </aop:config> </beans>
这样在com.spring.service包及其子类包都被事务管理了, 可更改<tx:method name="*"/>,更合理支持事务传播。
二、注解方式
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <context:component-scan base-package="com.spring"></context:component-scan> <!-- 1,引入外部contextSchema命名空间; 2,导入外部配置文件 --> <context:property-placeholder location="classpath:./jdbc.properties" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 创建spring NamedParameter JdbcTemplate --> <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate"> <constructor-arg name="dataSource" ref="dataSource"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </bean>
并且要在支持事务管理的类或者方法上加注解:
@Transactional
参考文章:
http://blog.sina.cn/dpool/blog/s/blog_7ffb8dd501014d8d.html
http://lzh166.iteye.com/blog/1134146
http://www.cnblogs.com/rushoooooo/archive/2011/08/28/2155960.html
相关推荐
Spring中事务的传播属性详解,Spring中事务的传播属性详解
spring 事务传播 demo
spring事务的传播特性和事务隔离级别
通过代码解析spring传播特性,包括 1、Propagation.REQUIRED 方法被调用时自动开启事务,在事务范围内使用则使用同一个事务,否则开启新事务。 2、Propagation.REQUIRES_NEW 无论何时自身都会开启事务 3、...
Spring事务的传播特性和隔离级别 事务的几种传播特性详细介绍
系统理解spring事务传播属性,和隔离级别
事务传播特性&事务隔离级别 详细的事务传播特性&事务隔离级别
spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读 spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读
事务的属性:1.Propagation:用来设置事务的传播行为事务的传播行为:一个方法运行在了一个开启了事务的方法中时,当前方法是使用原来的事务还是开启了一个新
spring传播机制个人学习
可以在spring中对整个项目的事务进行控制
本文介绍spring事务传播的几种方式
Spring事务传播机制是指在多个事务操作发生时,如何管理这些操作之间的事务关系。Spring事务传播机制可以通过Propagation枚举类中的不同值来指定,共包括七种不同的传播行为。具体来说,Spring事务传播机制包括以下...
此ppt中前半部分通过spring事务的60道题的测试,摸底对事务的掌握情况,后半部分,对spring中的事务属性(传播行为、隔离级别、回滚规则、事务超时、是否只读)进行说明
Spring事务传播属性,TransactionDefinition接口中定义的事务传播属性.TransactionDefinition接口中定义的事务传播属性.
Spring在Transaction事务传播行为种类,希望对大家有所帮助
Spring事务传播原理及数据库事务操作原理.rar
Spring事务传播类型.md
spring事务的传播特性.pptx