spring声明式事务管理(基于TransactionProxyFactoryBean的方式)

以银行转账为例(本文只作为自己学习spring的备忘录,只对自己负责)

转账的dao层接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.slowly.springtrancation01;  
  2.   
  3. /** 
  4.  * 转账的dao层接口 
  5.  * @author hy 
  6.  * 
  7.  */  
  8. public interface accountDao {  
  9.     /** 
  10.      *  
  11.      * @param out 转出账户 
  12.      * @param money 转出金额 
  13.      */  
  14.     public void outMoney(String out,Double money);  
  15.       
  16.     /** 
  17.      *  
  18.      * @param in 转入账户 
  19.      * @param money 转入金额 
  20.      */  
  21.     public void inMoney(String in,Double money);  
  22. }  

转账dao层接口实现类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.slowly.springtrancation01;  
  2.   
  3. import org.springframework.jdbc.core.support.JdbcDaoSupport;  
  4.   
  5. public class accountDaoImp extends JdbcDaoSupport implements accountDao {  
  6.   
  7.     @Override  
  8.     public void outMoney(String out, Double money) {  
  9.         String sql = "update account set balance = balance-? where account_name = ?";  
  10.         this.getJdbcTemplate().update(sql, money,out);  
  11.   
  12.     }  
  13.   
  14.     @Override  
  15.     public void inMoney(String in, Double money) {  
  16.         String sql = "update account set balance = balance+? where account_name = ?";  
  17.         this.getJdbcTemplate().update(sql, money,in);  
  18.   
  19.     }  
  20.   
  21. }  


转账的业务逻辑层接口

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.slowly.springtrancation01;  
  2. /* 
  3.  * 转账的业务层的接口 
  4.  */  
  5. public interface accountService {  
  6.     /** 
  7.      *  
  8.      * @param out 转出账户 
  9.      * @param in 转入账户 
  10.      * @param money 转入的金额 
  11.      */  
  12.     public void transfer(String out,String in,Double money);  
  13. }  

业务逻辑接口实现类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.slowly.springtrancation01;  
  2.   
  3. import org.springframework.transaction.TransactionStatus;  
  4. import org.springframework.transaction.support.TransactionCallbackWithoutResult;  
  5. import org.springframework.transaction.support.TransactionTemplate;  
  6.   
  7. public class accountServiceImp implements accountService {  
  8.       
  9.     private accountDao accountDao;  
  10.       
  11.       
  12.   
  13.     public void setAccountDao(accountDao accountDao) {  
  14.         this.accountDao = accountDao;  
  15.     }  
  16.   

  17.   
  18.   
  19.     @Override  
  20.     public void transfer(String out, String in, Double money) {  
  21.    
  22.                 accountDao.outMoney(out, money);  
  23.                 //int i = 1/0;  
  24.                 accountDao.inMoney(in, money);   
  25.       
  26.     }  
  27.   
  28. }  


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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.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="${driverClass}"></property>
        	<property name="jdbcUrl" value="${jdbcUrl}"></property>
        	<property name="user" value="${user}"></property>
        	<property name="password" value="${password}"></property>
        </bean>
        <!-- 配置业务层 -->
        <bean id="accountService" class="com.slowly.springtrancation02.accountServiceImp">
        	<property name="accountDao" ref="accountDao"></property>
        </bean>
        
        <!-- 配置dao层 -->
        <bean id="accountDao" class="com.slowly.springtrancation02.accountDaoImp">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 事务管理配置 -->
        <bean id="tranctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 配置业务层的代理 -->
        <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        	<!-- 配置目标对象 -->
        	<property name="target" ref="accountService"/>
        	<!-- 配置事务管理器 -->
        	<property name="transactionManager" ref="tranctionManager"/>
        	
 			<!-- <property name="proxyTargetClass" value="true"></property> -->
        	<!-- 注入事务属性 -->
        	<property name="transactionAttributes">
        		<props>
        			<prop key="transfer">PROPAGATION_REQUIRED</prop>
        		</props>
        	</property>
        	
        </bean>
       <!--   <bean id="myProxy" parent="accountServiceProxy">
   			<property name="target" ref="accountService"/>
 		</bean> -->


</beans>


测试

package com.slowly.springtrancation02;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext2.xml");
		accountService accountService = (com.slowly.springtrancation02.accountService) apt.getBean("accountServiceProxy");
		accountService.transfer("乙", "甲", 600.00);

	}

}



测试用的和上文一样的数据库表

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值