Spring的事务也可以通过AOP来实现,可以借助AOP实现切面事务功能,需要使用TransactionProxyFactoryBean代理类。TransactionProxyFactoryBean代理通过切面增强来对被代理的目标对象中的方法进行事务控制。下面我们通过一个示例来演示下用法。
代理类org.springframework.transaction.interceptor.TransactionProxyFactoryBean
创建表:
CREATE TABLE `useraccount` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`money` double DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
引入JAR包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
持久层
package com.trans.trans02;
public interface IUserAccountDao {
//转出账户
public void TransferOut(String user,double money);
//转入账户
public void TransferIn(String user,double money);
}
package com.trans.trans02;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class UserAccountDao extends JdbcDaoSupport implements IUserAccountDao {
public void TransferOut(String user, double money) {
// TODO Auto-generated method stub
String sql="UPDATE `useraccount` SET money=money-? WHERE username=?";
this.getJdbcTemplate().update(sql, money,user);
}
public void TransferIn(String user, double money) {
// TODO Auto-generated method stub
String sql="UPDATE `useraccount` SET money=money+? WHERE username=?";
this.getJdbcTemplate().update(sql, money,user);
}
}
业务层
package com.trans.trans02;
public interface IUserAccoutService {
//fromAccount来源账户 toAccount目标账户,money金额
public void UserTransferMoney(String fromAccount,String toAccount,double money);
}
package com.trans.trans02;
import javax.annotation.Resource;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class UserAccoutService implements IUserAccoutService {
@Resource(name="userAccountDao")
private IUserAccountDao userAccountDao;
public void UserTransferMoney(String fromAccount,String toAccount,
double money) {
// org.springframework.transaction.TransactionDefinition.ISOLATION_REPEATABLE_READ
userAccountDao.TransferOut(fromAccount, money);//转出账户
int m=5/0; //出现异常
userAccountDao.TransferIn(toAccount, money);//转入账户
}
}
配置bean
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:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "
default-lazy-init="true">
<description>Spring配置</description>
<context:annotation-config />
<!-- 引入数据源配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 持久层 -->
<bean id="userAccountDao" class="com.trans.trans02.UserAccountDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userAccoutService" class="com.trans.trans02.UserAccoutService">
</bean>
<!-- 配置事务管理其 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据连接池 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- aop代理 -->
<bean id="userAccountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 设置被代理对象 -->
<property name="target" ref="userAccoutService"></property>
<!-- 设置事务管理其 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 设置事务属性 -->
<property name="transactionAttributes">
<props>
<!-- 设置拦截方法,可以使用通配符 -->
<!-- 可以设置事务的传播行为,隔离级别,多个值用逗号隔开 -->
<prop key="UserTransferMoney">PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ</prop>
</props>
</property>
</bean>
</beans>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/erp?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
运行
package com.trans.trans02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取代理类
IUserAccoutService service=(IUserAccoutService)context.getBean("userAccountServiceProxy");
service.UserTransferMoney("001", "002", 100);
}
}
demo下载http://download.csdn.net/detail/zx13525079024/9590746