Spring Mvc那点事---(34)Spring事务基于AspectJ实现

   Spring事务也可以通过AspectJ切面来实现,通过配置切点和事务通知来实现控制事务。下面我们看看使用AspectJ来怎么控制事务。

CREATE TABLE `useraccount` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `money` double DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
引入JAR包

<dependencies>
  
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.36</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.5</version>
</dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
  </dependencies>

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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "
    default-lazy-init="true">

    <description>Spring配置</description>
    <context:annotation-config />
<!-- 引入数据源配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
  
  <!-- 连接池 -->
  <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="userAccountDao" class="com.trans.trans03.UserAccountDao"> 
      <property name="dataSource" ref="dataSource"></property>
  </bean>
  
  <bean id="userAccoutService" class="com.trans.trans03.UserAccoutService">
  </bean>
  
  <!-- 配置事务管理其 -->
  
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据连接池 -->
        <property name="dataSource" ref="dataSource"></property>
  </bean>
  
  <!-- 配置事务通知 -->
  <tx:advice id="advice" transaction-manager="transactionManager">
   <tx:attributes>
     <!-- 设置事务方法 -->
     <tx:method name="UserTransferMoney" propagation="REQUIRED"/>
   </tx:attributes>
  </tx:advice>
  
  <!-- 配置切面 -->
  <aop:config>
    <aop:pointcut expression="execution(* com.trans.trans03.IUserAccoutService+.*(..) )" id="pointcut"/>
    <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
  </aop:config>
 
</beans>

持久层

package com.trans.trans03;

public interface IUserAccountDao {

	//转出账户
	public void TransferOut(String user,double money);
	
	//转入账户
	public void TransferIn(String user,double money);
	
	

}

package com.trans.trans03;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class UserAccountDao extends JdbcDaoSupport implements IUserAccountDao {

	public void TransferOut(String user, double money) {
		// TODO Auto-generated method stub
		String sql="UPDATE `useraccount` SET money=money-? WHERE username=?";
		this.getJdbcTemplate().update(sql, money,user);
	}

	public void TransferIn(String user, double money) {
		// TODO Auto-generated method stub
		String sql="UPDATE `useraccount` SET money=money+? WHERE username=?";
		this.getJdbcTemplate().update(sql, money,user);
		
	}



}
业务层

package com.trans.trans03;

public interface IUserAccoutService {

	//fromAccount来源账户  toAccount目标账户,money金额
	public void UserTransferMoney(String fromAccount,String toAccount,double money);
}

package com.trans.trans03;

import javax.annotation.Resource;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;



public class UserAccoutService implements IUserAccoutService {

	@Resource(name="userAccountDao")
	private IUserAccountDao userAccountDao;
	
  
	
	public void UserTransferMoney(String fromAccount,String toAccount,
		 	double money) {
            //  org.springframework.transaction.TransactionDefinition.ISOLATION_REPEATABLE_READ
				userAccountDao.TransferOut(fromAccount, money);//转出账户
				int m=5/0; //出现异常
				userAccountDao.TransferIn(toAccount, money);//转入账户

	}

}

开始转账
package com.trans.trans03;

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



/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {

    	ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 
    	
    	//获取代理类
    	IUserAccoutService service=(IUserAccoutService)context.getBean("userAccoutService");  
    	service.UserTransferMoney("001", "002", 100);
    }
}


demo下载

http://download.csdn.net/detail/zx13525079024/9590753


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值