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

使用注解配置声明式事务控制

其实主要一个注解  一步配置 即可:

一个注解:@Transactional(···)

① 使用 @Transactional 在需要进行事务控制的类或是方法上修饰(切点)注解可用的属性同 xml 配置方式,例如隔离 级别、传播行为等(直接注解中括号上配置即可)

注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置。(如果注解使用在类上 那么 全部类下面的方法都是按照这个注解为标准)

使用在方法上,不同的方法可以采用不同的事务参数配置。  (如果注解使用在 方法上 那么 不按照类上 的注解来,简单点说就是 : 就近原则

一步配置:配置事务注解驱动

④ Xml配置文件中要开启事务的注解驱动

下面代码演示:

类中有很多切点:

package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
@Transactional(isolation = Isolation.REPEATABLE_READ)
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao 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);
    }

    //@Transactional(isolation = Isolation.DEFAULT)
    public void xxx(){}
}

完全可以看到 注解@Transactional 可以在类上定义 也可以在方法上,而且 参数(事务属性信息)是直接写到其中。

这就是 “一个注解”,下面是 “一步配置” 配置 事务注解驱动:

<?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/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
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

    <!--组件扫描-->
    <context:component-scan base-package="com.itheima"/>

    <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/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

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

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>


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

</beans>

可以看到事务注解驱动的配置 是  <tx:annotation-driven/> 这个标签, 属性有transaction-manager,他配置的是 平台事务管理器,可以看到 直接引用上面配好的 即可。

这就是基于注解配置声明式事务控制(AOP底层)。

本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15050938.html

原文地址:https://www.cnblogs.com/bi-hu/p/15050938.html