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

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

转账的dao层接口

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. package com.slowly.springtrancation03;  
  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.springtrancation03;  
  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.springtrancation03;  
  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.springtrancation03;  
  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.springtrancation03.accountServiceImp">
            	<property name="accountDao" ref="accountDao"></property>
            </bean>
            
            <!-- 配置dao层 -->
            <bean id="accountDao" class="com.slowly.springtrancation03.accountDaoImp">
            	<property name="dataSource" ref="dataSource"></property>
            </bean>
            
            <!-- 事务管理配置 -->
            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            	<property name="dataSource" ref="dataSource"></property>
            </bean>
            
        <!-- 配置事务的通知,(事务的增强) -->
        	<tx:advice id="txAdvice" transaction-manager="transactionManager">
        		<tx:attributes>
        			<tx:method name="transfer" propagation="REQUIRED"/>
        		</tx:attributes>
        	</tx:advice>
        	
        	<!-- 配置切面 -->
        	<aop:config>
        		<!-- 配置切入点 -->
        		<aop:pointcut expression="execution(* com.slowly.springtrancation03.accountService+.*(..))" id="pointcut01"/>
        		<!-- 配置切面 -->
        		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut01"/>
        	</aop:config>
    
    </beans>


    测试

    [java]  view plain  copy
      在CODE上查看代码片 派生到我的代码片
    1. package com.slowly.springtrancation03;  
    2.   
    3. import org.springframework.context.ApplicationContext;  
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    5.   
    6. public class Test {  
    7.   
    8.     public static void main(String[] args) {  
    9.         ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext3.xml");  
    [java]  view plain  copy
      在CODE上查看代码片 派生到我的代码片
    1.     accountService accountService = (com.slowly.springtrancation03.accountService) apt.getBean("accountServiceProxy");  
    2.     accountService.transfer("乙""甲"600.00);  
    3.   
    4. }  



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值