开发者

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

开发者 https://www.devze.com 2022-12-27 15:41 出处:网络
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 service

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 followin开发者_JS百科g :- 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消