Spring对声明式事务的支持

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/loveyouyuan/article/details/102563524

Spring对声明式事务的支持

Spring的事务控制首先明确

一般我们对数据库数据进行管理的时候,很多时候我们都需要对数据进行控制,事务的控制很关键,这些代码之间相互的很多都是重复的代码,不符合我们编程的思想,我们希望能够把这些相同的代码给提取出来,实现代码的可重复性,众所周知,我们处理业务逻辑的时候一般都牵扯到事务的需求,在这里我们就以转账的业务为例,JavaEE体系进行分层开发,事务处理为于业务层,Spring提供了分层设计业务层的事务处理解决方案,spirng的事务控制都是基于aop的,它即可以通过编程的方式来实现,也可以通过配置的方式来实现,我们这里重点是学习通过配置的方式来实现

Spring的基于xml的声明式事务的执行流程

1.配置事务管理器

2.配置事务的通知
此时事先需要导入事务的约束,tx的名称和空间,同时还需要aop的注解,使用tx:advice标签配置事务通知.属性id给事务起了一个唯一的标识,transaction-manager:给事务通知提供一个事务管理器引用

3.配置AOP的切入点表达式

4.建立通知和切入点的表达式

5.配置事务的属性

 <!--配置事务管理器(事务的切面类)-->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"></property>
     </bean>

     <!--配置事物的通知 transaction-manager配置是那个事务的特性-->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <!--配置事务的属性
             isolation:      用于指定事务的隔离级别,默认的是default,表示使用数据的隔离级别
             propagation:    用于指定事务的传播行为,默认是required,表示一定有事务,如果有事务,就执行当前的事务,如果没有就开启
                                   增删改的选择,查询方法可以选择的是supports,当前的事务的环境,

             read-only:      用于事务是否只读,只有查询方法才能设置为true,默认是false,表示读写
             timeout:        用于指定事务的超时时间,默认是-1,表示永不超时,如果指定的数据,以秒为单位
             rollback-for:   用于指定一个异常,当该异常产生时,事务回滚,产生其他异常时,事务不回滚,没有默认值,表示任何异常都回滚
             no-rollback-for:用于指定一个异常,当该异常产生时,事务不回滚,产生其他异常时,事务回滚,没有默认值,表示任何异常都回滚
          -->
          <tx:attributes>
               <tx:method name="*" propagation="REQUIRED" read-only="false"/>
               <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
          </tx:attributes>
     </tx:advice>

     <!--配置AOP-->
     <aop:config>
          <!--配置切入点表达式-->
          <aop:pointcut id="pt1" expression="execution(* cn.ujiuye.service.impl.*.*(..))"/>
          <!--建立切入点表达式和事务通知的对应的关系-->
          <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
     </aop:config>

Spring的基于xml的声明式事务的代码体现

1.配置bean.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: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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

     <!--配置业务层-->
     <bean id="accountService" class="cn.ujiuye.service.impl.AccountServiceImpl">
          <property name="accountDao" ref="accountDao"></property>
     </bean>

     <!--配置账号的持久层-->
     <bean id="accountDao" class="cn.ujiuye.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
     </bean>

     <!--配置数据源,配置spring中的数据源-->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
          <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
          <property name="username" value="root"></property>
          <property name="password" value="123"></property>
     </bean>

     <!--spring中基于xml的声明式事务控制的配置过程
         1.配置事物管理器
         2.配置事务的通知
             此时事先导入事务的约束 tx的名称和空间,同时还需要aop的
             使用tx:advice标签配置事务通知
                属性:
                    id:给事务起一个唯一标志
                    transaction-manager:给事务通知提供一个事务管理器引用
         3.配置AOP中的通用切入点表达式
         4.建立通知和切入点的表达式的对应关系
         5.配置事务的属性

         通知管理器在准守事务的通知的配置后,作用在切点上,完成事务的特点

     -->
     <!--配置事务管理器(事务的切面类)-->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource"></property>
     </bean>

     <!--配置事物的通知 transaction-manager配置是那个事务的特性-->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <!--配置事务的属性
             isolation:      用于指定事务的隔离级别,默认的是default,表示使用数据的隔离级别
             propagation:    用于指定事务的传播行为,默认是required,表示一定有事务,如果有事务,就执行当前的事务,如果没有就开启
                                   增删改的选择,查询方法可以选择的是supports,当前的事务的环境,

             read-only:      用于事务是否只读,只有查询方法才能设置为true,默认是false,表示读写
             timeout:        用于指定事务的超时时间,默认是-1,表示永不超时,如果指定的数据,以秒为单位
             rollback-for:   用于指定一个异常,当该异常产生时,事务回滚,产生其他异常时,事务不回滚,没有默认值,表示任何异常都回滚
             no-rollback-for:用于指定一个异常,当该异常产生时,事务不回滚,产生其他异常时,事务回滚,没有默认值,表示任何异常都回滚
          -->
          <tx:attributes>
               <tx:method name="*" propagation="REQUIRED" read-only="false"/>
               <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
          </tx:attributes>
     </tx:advice>

     <!--配置AOP-->
     <aop:config>
          <!--配置切入点表达式-->
          <aop:pointcut id="pt1" expression="execution(* cn.ujiuye.service.impl.*.*(..))"/>
          <!--建立切入点表达式和事务通知的对应的关系-->
          <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
     </aop:config>
</beans>

2.定义其实现的过程

package cn.ujiuye.service.impl;

import cn.ujiuye.dao.IAccountDao;
import cn.ujiuye.domain.Account;
import cn.ujiuye.service.IAccountService;


import java.util.List;

/**
 * @author liugang
 * @date 2019/10/5
 * 账号的业务逻辑层的实现类
 * 事务的业务逻辑也是在这层来控制的
 */
public class AccountServiceImpl implements IAccountService {

    /*使用的是set注入的方法来实现注入accountDao数据*/
    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao){
        this.accountDao = accountDao;
    }

    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);

    }


    public void transfer(String sourceName, String targetName, Float money) {


        System.out.println("transfer....");
        //2.1根据名称查询转出账户
        Account source = accountDao.findAccountByName(sourceName);
        //2.2根据名称查询转入账户
        Account target = accountDao.findAccountByName(targetName);
        //2.3转出账户减钱
        source.setMoney(source.getMoney()-money);
        //2.4转入账户加钱
        target.setMoney(target.getMoney()+money);
        //2.5更新转出账户
        accountDao.updateAccount(source);

        int i=1/0;

        //2.6更新转入账户
        accountDao.updateAccount(target);
    }
}

3.书写测试类

package cn.ujiuye.test;

import cn.ujiuye.domain.Account;
import cn.ujiuye.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * @author liugang
 * @date 2019/10/5
 * 使用Junit单元测试,测试我们的配置
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceImplTest {

    @Autowired
    private IAccountService as;
    @Test
    public void testTransfer(){
        as.transfer("aaa","bbb",100f);
    }

}

猜你喜欢

转载自blog.csdn.net/loveyouyuan/article/details/102563524