spring再学习之AOP事务

Posted 小南天门

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring再学习之AOP事务相关的知识,希望对你有一定的参考价值。

spring中的事务

spring怎么操作事务的:

 

 

 

 事务的转播行为:

 

事务代码转账操作如下:

接口:

public interface AccountDao {
    
    //加钱
    void addMoney(Integer id,Double money);
    //减钱
    void decreaseMoney(Integer id,Double Money);
    

}

实现类:

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

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public void addMoney(Integer id, Double money) {
        String sql="update t_account set money=money+? where id = ?"; 
        getJdbcTemplate().update(sql,money,id);
        // TODO Auto-generated method stub

    }

    @Override
    public void decreaseMoney(Integer id, Double money) {
        String sql="update t_account set money=money-? where id = ?"; 
        getJdbcTemplate().update(sql,money,id);

    }

}

service层接口

package cn.itcast.service;

public interface AccountService {
    
    //转账方法
    void transfer(Integer from,Integer to,Double money);

}

service实现类:

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import cn.itcast.dao.AccountDao;
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public class AccountServiceImpl implements AccountService {

    
    private AccountDao ad;
    private TransactionTemplate tt;

    @Override
    
    public void transfer(final Integer from, final Integer to,final  Double money) {
               //事务
                //减钱
                ad.decreaseMoney(from, money);
                //加钱
                ad.addMoney(to, money);
        
    }
    
    public AccountDao getAd() {
        return ad;
    }
    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

    public TransactionTemplate getTt() {
        return tt;
    }

    public void setTt(TransactionTemplate tt) {
        this.tt = tt;
    }
    
/*    @Override
手动代码: public void transfer(final Integer from, final Integer to,final Double money) { //事务 tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus arg0) { // TODO Auto-generated method stub //减钱 ad.decreaseMoney(from, money); int i=1/0; //加钱 ad.addMoney(to, money); } }); }
*/ }

分析:

XML配置式实现事务:

原理;

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns
="http://www.springframework.org/schema/beans"
xmlns:p
="http://www.springframework.org/schema/p"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:tx
="http://www.springframework.org/schema/tx"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "
> <!--指定Spring读取db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 事物核心管理器,
封装了所有事物操作,
依赖于连接池
--> <bean name="dataSourceTransactionManager"
class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务模板对象 --> <bean name="transactionTemplate"
class
="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="dataSourceTransactionManager"></property> </bean> <!-- 配置事务通知
isolation:隔离级别
propagation:传播行为
read-only:是否只读
--> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <!-- 配置织入 --> <aop:config> <aop:pointcut expression="execution(* cn.itcast.service..*ServiceImpl.*(..))" id="txPc"/> <!-- 配置切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/> </aop:config> <!-- 1.将连接池交给Spring管理 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 2.将JDBCTemplate放入Spring容器 <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> --> <!-- 3.将accountDao放入spring容器 --> <bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3.将accountService放入spring容器 --> <bean name="accountService" class="cn.itcast.service.AccountServiceImpl"> <property name="ad" ref="accountDao"></property> <property name="tt" ref="transactionTemplate"></property> </bean> </beans>

 

xml配置式,中java写法:

1、打开事务

2、调用doInTransactionWithoutResult

3、提交事务

package cn.itcast.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import cn.itcast.dao.AccountDao;public class AccountServiceImpl implements AccountService {

    
    private AccountDao ad;
    private TransactionTemplate tt;

    @Override
    
    public void transfer(final Integer from, final Integer to,final  Double money) {
               //事务
                //减钱
                ad.decreaseMoney(from, money);
                //加钱
                ad.addMoney(to, money);
        
    }
    
    public AccountDao getAd() {
        return ad;
    }
    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

    public TransactionTemplate getTt() {
        return tt;
    }

    public void setTt(TransactionTemplate tt) {
        this.tt = tt;
    }
}

测试:

package cn.itcast.tx;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.itcast.service.AccountService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext1.xml")

public class Demo {
    
    @Resource(name="accountService")
    private AccountService ad;
    @Test
    public void fun1() {
        ad.transfer(1, 2, 1000.00);
    }
 
}

注解式配置事务:

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns
="http://www.springframework.org/schema/beans"
xmlns:p
="http://www.springframework.org/schema/p"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:tx
="http://www.springframework.org/schema/tx"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "
> <!--指定Spring读取db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 事物核心管理器,封装了所有事物操作,依赖于连接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事务模板对象 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"></property> </bean> <!--开启使用注解配置事务 --> <tx:annotation-driven /> <!-- 1.将连接池交给Spring管理 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 2.将JDBCTemplate放入Spring容器 <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> --> <!-- 3.将accountDao放入spring容器 --> <bean name="accountDao" class="cn.itcast.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3.将accountService放入spring容器 --> <bean name="accountService" class="cn.itcast.service.AccountServiceImpl"> <property name="ad" ref="accountDao"></property> <property name="tt" ref="transactionTemplate"></property> </bean> </beans>

 使用注解:

package cn.itcast.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import cn.itcast.dao.AccountDao;public class AccountServiceImpl implements AccountService {
private AccountDao ad; private TransactionTemplate tt; @Override @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void transfer(final Integer from, final Integer to,final Double money) { //事务 //减钱 ad.decreaseMoney(from, money); //加钱 ad.addMoney(to, money); } public AccountDao getAd() { return ad; } public void setAd(AccountDao ad) { this.ad = ad; } public TransactionTemplate getTt() { return tt; } public void setTt(TransactionTemplate tt) { this.tt = tt; } }

测试:

package cn.itcast.tx;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.itcast.service.AccountService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")

public class Demo {
    
    @Resource(name="accountService")
    private AccountService ad;
    @Test
    public void fun1() {
        ad.transfer(1, 2, 1000.00);
    }
 
}

 

结果:

 

以上是关于spring再学习之AOP事务的主要内容,如果未能解决你的问题,请参考以下文章

spring再学习之AOP实操

spring再学习之基本概念

spring 学习之二 AOP编程

Spring阶段性学习总结AOP编程入门学习之动态代理实现代码的优化

spring事务管理aop

手写Spring事务框架