TransactionProxyFactoryBean 配置事务 不回滚

在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”

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值