Spring中开启事务的第三种:注解配置

本文介绍了在Spring框架中启用事务管理的第三种方式——使用注解配置。首先,需要引入相关依赖,包括spring-aop、spring-aspects、aopalliance、aspectjweaver、spring-jdbc和spring-tx等jar包。接着,要在配置文件中开启对事务注解的支持。然后,可以将@Transactional注解添加到方法或类上来标记事务边界。最后,展示了如何在service类和spring配置文件中应用这些注解。

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

Spring中开启事务的第三种:注解配置
步骤
导包

spring-aop-4.3.8.RELEASE.jar
spring-aspects-4.3.8.RELEASE.jar
联盟包:com.springsource.org.aopalliance-1.0.0.jar
织入包:com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-jdbc-4.3.8.RELEASE.jar
spring-tx-4.3.8.RELEASE.jar
导入tx约束

spring-tx-4.3.xsd
xmlns:tx=”http://www.springframework.org/schema/tx”

在配置文件中开启对事务注解的支持

<!-- 开启spring对事务的注解支持 -->
<tx:annotation-driven/>

在方法或者类上添加注解

//可以在类,属性,方法上都可以添加注解,小范围覆盖大范围的
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements IAccountService{
    ......
}

service类

//可以在类,属性,方法上都可以添加注解,小范围覆盖大范围的
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements IAccountService{

    private AccountDaoImpl ac;

    @Override
    //可以在类,属性,方法上都可以添加注解,小范围覆盖大范围的
    @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
    public void transfer(final Integer from,final Integer to,final Double money) {

        //加钱
        ac.addMoney(to, money);
//      int i=1/0;
        //减钱
        ac.decreseMoney(from, money);
    }
    public void setAc(AccountDaoImpl ac) {
        this.ac = ac;
    }

spring配置文件。

<?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 ">

    <!-- 加载jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

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

    <!-- 开启spring对事务的注解支持 -->
    <tx:annotation-driven/>


    <bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
        <property name="ac" ref="accountDaoImpl"></property>
    </bean>

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

    <!-- 配置模板对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值