Spring transactions - Mixing @Transactional with <tx:advice> into a custom annotation

核能气质少年 提交于 2019-12-23 02:34:27

问题


My goal is to have some way of declaring my service classes as transactional. I dont want to leave it as an explicit declaration in spring configuration. Many times in past we have created new services and forgot to declare transactions around them. Hence my intention is that if i have something like @TransactionalService custom annotation, it should do the following :- 1. provides transactional support 2. declares some default rules for transactional support as spring currently provides as shown below. But unlike spring, i would like the below to be part of my @TransactionService annotation.

<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="*"/>

Any advice would be valuable?


回答1:


Sure, instead of creating a new annotation, you could just put your transactionnal services in the same package, and then your pointcut (only one for all you transactionnal services) will look like this :

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

The advice is the same as above :

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


来源:https://stackoverflow.com/questions/2669329/spring-transactions-mixing-transactional-with-txadvice-into-a-custom-annot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!