TransactionProxyFactoryBean 配置事务 不回滚

本文深入探讨了Spring框架中事务管理的配置与实现细节,通过一个实际案例,揭示了正确使用TransactionProxyFactoryBean进行事务增强的方法,以及如何避免常见的注入错误。

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

在Spring 4.x一书中,11.5.2 使用原始的TransactionProxyFactoryBean 配置事务,代码如下。

仿照书本代码写一个demo,发现事务并没有回滚。

经过查阅,发现是测试类注入目标业务Bean出错了。配置事务增强后,要使配置生效,调用目标Bean的时候,要注入其代理类。

<bean id="customerService" class="com.gpj.service.CustomerService"
p:customerMapper-ref="customerMapper"/>

<bean id="customerServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    p:target-ref="customerService"
    p:transactionManager-ref="txManager">
    <property name="transactionAttributes">
      <props>
        <prop key="testTx">PROPAGATION_REQUIRED,-NullPointerException</prop>
        <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
      </props>
    </property>
  </bean>

 @Resource(name = "customerServiceProxy")
    private CustomerService customerService;

    @Test
    public void testSpring(){
        customerService.testTx();
    }

 需要实施事务增强的目标业务Bean:CustomerService

 为目标业务Bean提供事务增强代理类的id:customerServiceProxy

所以要使用业务Bean的时候,注入Bean要指定name:“customerServiceProxy”

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抹除不掉的轻狂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值