基于注解的声明式事务配置

基于注解的声明式事务配置

文章目录

1.概述

在Spring中,可以使用XML和注解进行声明式事务配置.
在进行注解配置时,一定要会XML配置,明白其中的道理.

2.配置过程

例子:转账

1.导入坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-transaction-anno</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--1.spring上下文-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--2.spring测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--3.数据库相关-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--c3p0连接池-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!--jdbc工具-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--4.切面-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
        <!--5.事务-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2.配置XML(或者使用新注解进行纯注解开发)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/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
">
    <!--扫com.itspring.tx.anno包下的所有类-->
    <context:component-scan base-package="com.itspring.tx.anno"/>
    <!--配置切面自动代理-->
    <aop:aspectj-autoproxy/>
    <!--引入jdbc.properties-->
    <context:property-placeholder location="jdbc.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--jdbc的事务管理器-->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=root

3.domain,dao,service

//--------domain:Account---------------



//-------dao:interface:AccountDao-----------
package com.itspring.tx.anno.dao;


import com.itspring.tx.anno.domain.Account;

public interface AccountDao {
    int update(Account account, Double n);
}

//-------daoImpl:AccountDaoImpl-----------
package com.itspring.tx.anno.dao.impl;


import com.itspring.tx.anno.dao.AccountDao;
import com.itspring.tx.anno.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int update(Account account, Double n) {
        String sql = "update account set money = money+? where id=?";
        int result = jdbcTemplate.update(sql, n, account.getId());
        System.out.println(result);
        return result;
    }
}

//-------service:interface:AccountService-----------
package com.itspring.tx.anno.service;


import com.itspring.tx.anno.domain.Account;

public interface AccountService {
    //a1给a2转n元
    boolean transfer(Account a1, Account a2, Double n);
}

//-------serviceImpl:AccountServiceImpl-----------
package com.itspring.tx.anno.service.impl;


import com.itspring.tx.anno.dao.AccountDao;
import com.itspring.tx.anno.domain.Account;
import com.itspring.tx.anno.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
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    //事务注解,可以设置属性
    @Transactional(isolation = Isolation.DEFAULT,
            propagation = Propagation.REQUIRED,
            timeout = -1,
            readOnly = false)
    public boolean transfer(Account a1, Account a2, Double n) {
        //这是一个原子操作,要用事务控制原子性.
        System.out.println("开始转账...");
        accountDao.update(a1, -n);
        //设置一个异常
        int i = 1 / 0;
        accountDao.update(a2, n);
        System.out.println("转账完成...");
        return true;
    }
}

4.测试

package com.anno;

import com.itspring.tx.anno.domain.Account;
import com.itspring.tx.anno.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestService {

    @Autowired
    private AccountService accountService;

    @Test
    public void test1() {
        //此处假设已经获取到用户
        Account a1 = new Account(1, "张三", 400.0);
        Account a2 = new Account(2, "李四", 1100.0);
        //张三给李四转50
        this.accountService.transfer(a1, a2, 50.0);
    }
}

基于注解的声明式事务配置
基于注解的声明式事务配置

3.注解配置声明式事务控制解析

① 使用 @Transactional在需要进行事务控制的类或是方法上修饰,注解可用的属性同 xml 配置方式,例如隔离级别、传播行为等。
② 注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置。
③ 使用在方法上,不同的方法可以采用不同的事务参数配置。
④ Xml配置文件中要开启事务的注解驱动<tx:annotation-driven />

上一篇:‘HttpClientModule‘ declared by the module ‘AppModule‘. Please add a @Pipe/@Directive/@Component anno


下一篇:拟南芥基因注释