Spring的转账案例
1)代码准备
1)创建业务层
public interface AccountService { public void transfer(String from, String to, Double money); } ----------------------------------------------------------------------------------- public class AccountServiceImpl implements AccountService { // 业务层注入 DAO: private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override /** * from:转出的账号 * to:转入的账号 * money:转账金额 */ public void transfer(String from, String to, Double money) { accountDao.outMoney(from, money); accountDao.inMoney(to, money); } }
2)创建DAO类
public interface AccountDao { public void outMoney(String from, Double money); public void inMoney(String to, Double money); } ------------------------------------------------------------------------------- public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override public void outMoney(String from, Double money) { this.getJdbcTemplate().update("update account set money = money - ? where name = ? ", money,from); } @Override public void inMoney(String to, Double money) { this.getJdbcTemplate().update("update account set money = money + ? where name = ? ", money,to); } }
3)配置业务层和DAO类
<!-- 配置业务层的类 --> <bean id="accountService" class="cn.itcast.transaction.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> <!-- 配置 DAO 的类 --> <bean id="accountDao" class="cn.itcast.transaction.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean>
3)测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext2.xml") public class SpringDemo4 { @Resource(name = "accountService") private AccountService accountService; @Test // 转账的测试: public void demo1() { accountService.transfer("会希", "凤姐", 1000d); } }
第一种 - Spring的编程式事务
1)配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
2)配置事务管理模板
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean>
3)业务层注入事务管理模板
<!-- 配置业务层的类 --> <bean id="accountService" class="cn.itcast.transaction.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> <!-- 注入事务管理模板 --> <property name="transactionTemplate" ref="transactionTemplate"/> </bean>
4)代码实现
public void transfer(final String from, final String to, final Double money) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { accountDao.outMoney(from, money); int d = 1 / 0; accountDao.inMoney(to, money); } }); }
第二种 - Spring的声明式事务AOP(XML的方式)
1)配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
2)配置事务通知
<!-- 配置事务的增强 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- isolation="DEFAULT" 隔离级别 propagation="REQUIRED" 传播行为 read-only="false" 只读 timeout="-1" 过期时间 rollback-for="" -Exception no-rollback-for="" +Exception --> <tx:method name="transfer" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
3)配置AOP事务
<aop:config> <aop:pointcut expression="execution(*cn.itcast.transaction.demo2.AccountServiceImpl.transfer(..))" id="pointcut1"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> </aop:config>
第二种 - Spring的声明式事务AOP(注解的方式)
1)配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
2)开启事务管理的注解
<tx:annotation-driven transaction-manager="transactionManager"/>
3)在使用事务的类上添加一个注解:@Transactional