【java框架】Spring(6) -- Spring事务管理配置

1.Spring事务管理配置

本节主要讲解Spring声明式事务控制的配置和相关概述。以银行转账金额小案例说明事务提交、回滚中spring如何通过配置保持事务的一致性问题。具体实现及概念性问题不再过多阐述,主要简单说明具体实现业务配置。

1.1.声明式事务控制简介

Spring的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明,就是指在配置文件中声明,用在Spring配置文件中声明式的处理事务来代替代码式的处理事务。

Spring声明式事务控制底层用到的就是AOP的思想

1.2.基于XML的声明式事务控制

实现主要步骤如下:

①配置xml中引入spring-tx命名空间;

②pom.xml中配置spring-tx相关坐标;

③配置applicationContext.xml中的事务增强、aop织入;

④基于业务中的增强点、切入点方法进行测试;

最终配置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: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/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
">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springtxtest"/>
        <property name="user" value="root"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="accountDao" class="com.fengye.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <bean id="accountService" class="com.fengye.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--通知 事务增强-->
    <tx:advice id="txAdvice">
        <!--设置事务的属性信息-->
        <tx:attributes>
            <!--内部可以设置多个方法,对多个方法配置参数,如隔离级别、可读性-->
            <tx:method name="*"/>
            <tx:method name="transfer" isolation="READ_COMMITTED" propagation="REQUIRED" timeout="-1" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务aop的织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.fengye.service.impl.*.*(..))"></aop:advisor>
    </aop:config>

</beans>

配置pom.xml中坐标依赖如下:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.4</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

切点方法的事务参数配置在xml中如下:

<!--通知 事务增强-->
<tx:advice id="txAdvice">
    <!--设置事务的属性信息-->
    <tx:attributes>
        <!--内部可以设置多个方法,对多个方法配置参数,如隔离级别、可读性-->
        <tx:method name="*"/>
        <tx:method name="transfer" isolation="READ_COMMITTED" propagation="REQUIRED" timeout="-1" read-only="false"/>
    </tx:attributes>
</tx:advice>

其中,<tx:method>代表切点方法的事务参数的配置,例如:

<tx:method name="transfer" isolation="READ_COMMITTED" propagation="REQUIRED" timeout="-1" read-only="false"/>
  •   Name:切点方法名称
  •   Isolation:事务的隔离级别
  •   Propogation:事务的传播行为
  •   Timeout:超时时间
  •   Read-only:是否只读

1.3.基于注解的声明式事务控制

基于注解的事务控制步骤:

①配置xml中引入spring-tx/spring-context命名空间;

②pom.xml中配置spring-tx相关坐标;

③在对应的dao、service及切面类中的方法上加上注解@Transactional表示事务约束;

④基于业务中的增强点、切入点方法进行测试;

Dao、Service层主要配置注入如下:

AccountDao:

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }

    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}

AccountService:

@Service("accounrService")
//也可以加在类上面,表示当前类中的所有方法都配置这个事务隔离级别
//@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}

注意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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springtxtest"/>
        <property name="user" value="root"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置spring包注解扫描-->
    <context:component-scan base-package="com.fengye" />

    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--事务的注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

上述代码示例及建库sql均已上传至github:

https://github.com/devyf/SpringReview/tree/master/fengye_spring_tx

https://github.com/devyf/SpringReview/tree/master/fengye_spring_tx_anno

原文地址:https://www.cnblogs.com/yif0118/p/14403124.html