Spring事务管理(三)使用tx标签

本文详细介绍了Spring框架中使用tx标签进行事务管理的方法,包括配置详解、事务传播行为以及如何在实际应用中结合XML配置实现事务控制,帮助开发者更好地掌握Spring事务操作。

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

由于事务的配置方法较多,没有一一实践过,现在贴出平常常用的一种使用tx标签的配置方法,仅供参考。
<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    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 
    ">

    <!-- 此处的dataSource 为数据源配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>  

    <tx:advice id="dataAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="select*" propagation="SUPPORTS" />
            <tx:method name="query*" propagation="SUPPORTS" />
            <tx:method name="get*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <!-- 表示在com.xxx.manager包以及所有子包下的的任意类的任意方法 -->
        <aop:pointcut id="dataPointcut" expression="execution(* com.XXX.manager..*.*(..))" />
        <aop:advisor pointcut-ref="dataPointcut" advice-ref="dataAdvice" />
    </aop:config>

    <!-- 使用@Transactional 注解必须开启配置-->
    <!--<tx:annotation-driven transaction-manager="transactionManager"/>-->

</beans>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值