Spring通过事务管理器,以AOP方式对事务进行管理,以转账为例进行练习
Dao类
package com.maty;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月24日 上午10:54:57 类说明
*/
public class AccountDao
{
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
// 转出操作
public void lessMoney()
{
String sql = "update account set money=money-300 where username=?";
jdbcTemplate.update(sql, "Alisa");
}
// 转入操作
public void moreMoney()
{
String sql = "update account set money=money+300 where username=?";
jdbcTemplate.update(sql, "Bob");
}
}
Service类
package com.maty;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月24日 上午10:54:45 类说明
*/
@Transactional(value = "dataSourceTransactionManager")
public class AccountService
{
@Resource(name = "accountDao")
private AccountDao dao;
public void txMoney()
{
dao.lessMoney();
// 人为制造异常
// int i = 10 / 0;
dao.moreMoney();
System.out.println("转账成功");
}
}
XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!-- 开启注解的扫描 -->
<context:component-scan base-package="com.maty"></context:component-scan>
<!-- 注入c3p0的datasource -->
<bean id="driverManagerDataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/tx"></property>
<property name="user" value="root"></property>
<property name="password" value="wangxiaowei"></property>
</bean>
<!-- 注入jdbcTamplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 将datasource注入到模板中 -->
<property name="dataSource" ref="driverManagerDataSource"></property>
</bean>
<!-- 将Service和Dao类注入 -->
<bean id="accountDao" class="com.maty.AccountDao"></bean>
<bean id="accountService" class="com.maty.AccountService"></bean>
<!-- 进行spring的事务配置 -->
<!-- 第一步:配置事务管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 将c3p0的datasource纳入到事务管理器中 -->
<property name="dataSource" ref="driverManagerDataSource"></property>
</bean>
<!-- 第二步:开启事务注解 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
测试类
package com.maty.text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.maty.AccountService;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月24日 上午10:58:10 类说明
*/
public class MyTest
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService service = (AccountService) context.getBean("accountService");
service.txMoney();
}
}