spring事物管理--声明式(AspectJ)注解实现 (推荐使用)
1、表结构及数据
2、使用的jar包
3、service、Dao层接口与实现类:
Dao接口:
//转账案例持久层接口 public interface AccountDao { /** * @param out :转出账号 * @param money :转账金额 */ public void outMoney(String out,Double money); /** * @param in :转入账号 * @param money :转账金额 */ public void inMoney(String in,Double money); }
Dao实现类:
//转账案例持久层实现类 public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { /** * @param out :转出账号 * @param money :转账金额 */ @Override public void outMoney(String out, Double money) { String sql = "update account set money = money - ? where name = ?"; this.getJdbcTemplate().update(sql,money,out); } /** * @param in :转入账号 * @param money :转账金额 */ @Override public void inMoney(String in, Double money) { String sql = "update account set money = money + ? where name = ?"; this.getJdbcTemplate().update(sql,money,in); } }
service接口:
//转账案例业务层接口 public interface AccountService { /** * @param out :转出账号 * @param in :转入账号 * @param money :转账金额 */ public void transfer(String out,String in,Double money); }
service实现类:
使用@Transactional事物注解,根据自身需求可以使用不同的注解属性propagation、isolation、readOnly、rollbackFor、noRollbackFor
//转账案例业务层实现类 @Transactional(propagation=Propagation.REQUIRED)//使用注解实现事务管理,根据需求使用不同的注解属性 public class AccountServiceImpl implements AccountService { //注入转账的Dao @Resource private AccountDao accountDao; /** * @param out :转出账号 * @param in :转入账号 * @param money :转账金额 */ @Override public void transfer( String out, String in, Double money) { //把业务操作放入内部类中----在一个事物里面(同成功,同失败) accountDao.outMoney(out, money); int i = 1/0; //异常测试 accountDao.inMoney(in, money); } }
4、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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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-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"> <!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0连接池 --> <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="accountService" class="cn.xl.spring.demo4.AccountServiceImpl"> </bean> <!-- 配置持久层类 --> <bean id="accountDao" class="cn.xl.spring.demo4.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事物管理器配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启注解事物 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5、测试类:
//spring声明式事物管理的方式二的测试类:基于AspectJ的XML配置 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext4.xml") public class SpringDemo4 { //需注入业务层代理对象 @Resource(name="accountService") private AccountService accountService; @Test public void demo4(){ accountService.transfer("aaa", "bbb", 200d); } }