Spring事务管理使用注解远比使用配置方便,这里就演示一下如何使用注解:(Spring版本:3.1.1)
1>导入事务jar包:org.springframework.transaction-3.0.6.RELEASE.jar
2>修改Spring配置文件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:p="http://www.springframework.org/schema/p" 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/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" > <!--1. 添加事务schema(配置在上面) xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd --> <!--2. 开启事务的Annotation支持 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 3.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
3>在需要事务的类上面,只需要加上@Transactional,那么这个类的所有方法都是事务
--end---