【问题标题】:Spring transactions - Mixing @Transactional with <tx:advice> into a custom annotationSpring 事务 - 将 @Transactional 与 <tx:advice> 混合到自定义注释中
【发布时间】:2011-02-09 18:59:46
【问题描述】:

我的目标是通过某种方式将我的服务类声明为事务性的。我不想将它作为 spring 配置中的显式声明。过去很多时候,我们创建了新服务,却忘记声明围绕它们的交易。因此我的意图是,如果我有类似 @TransactionalService 自定义注释的东西,它应该执行以下操作:- 1. 提供事务支持 2. 声明一些默认的事务支持规则,正如spring目前提供的那样,如下图所示。但与 spring 不同,我希望以下内容成为我的 @TransactionService 注释的一部分。

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>

有什么有价值的建议吗?

【问题讨论】:

    标签: spring-transactions


    【解决方案1】:

    当然,您可以将事务服务放在同一个包中,而不是创建新注释,然后您的切入点(只有一个用于所有事务服务)将如下所示:

    <aop:config>
      <aop:pointcut id="transactionnalServiceMethods" expression="execution(* x.y.transactionnalservice.*.*(..))"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionnalServiceMethods"/>
    </aop:config>
    

    建议同上:

      <tx:advice id="txAdvice" transaction-manager="txManager">
      <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <tx:method name="get*" read-only="true"/>
        <tx:method name="*"/>
      </tx:attributes>
      </tx:advice>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-14
      • 2017-02-05
      • 2014-06-30
      • 2018-03-28
      • 1970-01-01
      • 2019-01-09
      • 2023-02-16
      • 1970-01-01
      相关资源
      最近更新 更多