Spring中的事务管理之声明式事务(基于注解)是基于<tx:annotation-driven/>,它默认查找名称为transactionmanager的事务管理器Bean。
这个很简单,请看例子。
首先,恢复项目环境。详见环境搭建。
1、配置applicationContxt.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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 定义JDBC的模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 业务层类 -->
<bean id="accountService" class="com.js.demo4.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 持久层类 -->
<bean id="accountDao" class="com.js.demo4.AccountDaoImpl">
<!-- 注入连接池对象,通过连接池对象去创建JDBC模板 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解的事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2、在Service上使用注解AccountServiceImpl:@Transactional
package com.js.demo4;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 转账的方法
* @param from:从哪转出
* @param to:转入目标
* @param money:转账金额
*/
public void transfer(String from, String to,double money) {
accountDao.out(from, money);
// int i = 4/0;
accountDao.in(to, money);
}
}
该注解有很多属性,大家使用的话就会发现属性名并不陌生:
3、进行测试:
(3.1)有异常:
package com.js.demo4;
import org.springframework.transaction.annotation.Transactional;
@Transactional()
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 转账的方法
* @param from:从哪转出
* @param to:转入目标
* @param money:转账金额
*/
public void transfer(String from, String to,double money) {
accountDao.out(from, money);
int i = 4/0;
accountDao.in(to, money);
}
}
package com.js.demo4;
import org.springframework.transaction.annotation.Transactional;
@Transactional()
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 转账的方法
* @param from:从哪转出
* @param to:转入目标
* @param money:转账金额
*/
public void transfer(String from, String to,double money) {
accountDao.out(from, money);
int i = 4/0;
accountDao.in(to, money);
}
}
(3.2)无异常:
package com.js.demo4;
import org.springframework.transaction.annotation.Transactional;
@Transactional()
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 转账的方法
* @param from:从哪转出
* @param to:转入目标
* @param money:转账金额
*/
public void transfer(String from, String to,double money) {
accountDao.out(from, money);
// int i = 4/0;
accountDao.in(to, money);
}
}
事务测试通过。使用起来很简单,不是吗?