SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务

本文介绍如何使用Spring框架通过注解的方式配置事务管理。主要内容包括引入必要的依赖包、定义AccountServiceImpl类并应用事务注解、配置Spring XML文件启用注解事务管理及创建事务模板对象、编写测试代码验证事务正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务

一、导包

Spring AOP包
“`xml
spring-aop

spring-aspects

org.aspectj.weaver

org.aopalliance
“`


二、AccountSeviceImpl类

Dao接口以及实现、Service接口和之前的是一样的,AccountSeviceImpl不一样,如下

事务的注解可以注解到类上,也可以注解到方法上,可以两者都注解。

package com.qwm.spring3.tx.service;

import com.qwm.spring3.tx.dao.AccountDao;
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;

/**
 * @author:qiwenming
 * @date:2017/10/31 0031   21:02
 * @description:
 */
@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)
public class AccountServiceImpl implements AccountService {

    private AccountDao ad;
    private TransactionTemplate tt;

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)
    public void updateMoney(Long fromId, Long toId, Double money) {
        //减钱
        ad.decreaseMoney(fromId,money);
//        int i = 1/0;
        //加钱
        ad.increaseMoney(toId,money);
    }

    public void setAd(AccountDao ad) {
        this.ad = ad;
    }

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

三、配置配置文件

3.1 开启注解管理事务

    <!-- 开启使用注解管理aop事务 -->
    <tx:annotation-driven/>

3.2 完整配置

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

    <!-- 1.指定spring读取db.properties配置-->
    <context:property-placeholder location="classpath:com/qwm/spring3/db.properties"/>

    <!--2.将连接池对象放入到spring容器中-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 3.将AccountDao放入到spring 容器中 -->
    <bean name="accountDao" class="com.qwm.spring3.tx.dao.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事务核心管理器,封装了所有事务操作,依赖于连接池-->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事务模板对象-->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <!-- 开启使用注解管理aop事务 -->
    <tx:annotation-driven/>

    <!--4.将AccountService放入到spring容器中-->
    <bean name="accountService" class="com.qwm.spring3.tx.service.AccountServiceImpl">
        <property name="ad" ref="accountDao"/>
        <property name="tt" ref="transactionTemplate"/>
    </bean>

</beans>

四、测试

测试之前,先把数据库中表的数据恢复到初始值

这里写图片描述

测试代码

/**
 * @author:qiwenming
 * @date:2017/10/31 0031   21:32
 * @description:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/qwm/spring3/tx/applicationContext.xml")
public class Demo {

    @Resource(name = "accountService")
    private AccountService as;

    @Test
    public void test1(){
        as.updateMoney(1L,2L,100D);
    }

}

结果

这里写图片描述


五、源码下载

https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring3/tx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值