2010-06-23 86 views

回答

8

是的,使用aop:configtx:advice。例如:

<aop:config> 
    <aop:pointcut id="serviceMethods" 
     expression="execution(* com.package.service..*.*(..))" /> 

    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> 
</aop:config> 

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
    <tx:attributes> 
     <tx:method name="*" propagation="REQUIRED" /> 
    </tx:attributes> 
</tx:advice> 
+1

是的,你可以在這裏看到不同的可能性:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative – 2010-06-23 07:41:39

3

註釋是標記應該在事務中執行方法的最佳選擇。這對於Spring和EJB 3都是推薦的。

XML方法需要更多的配置,不是重構友好的,您必須在配置中看到某個方法是否將在事務中執行。

由於基於註釋的事務支持是大多數開發人員的首選,而且您不喜歡使用Spring的@Transactional註釋,所以我會建議您使用自定義註釋。

這時你有兩種選擇:

  • 讓你自定義註解擴展Spring的@Transactional和Spring配置使用<tx:annotation-driven />元素。這很容易,只需要更新一個註釋來刪除Spring依賴項。
  • 創建一個在註釋方法之前和之後執行邏輯的攔截器。以Spring作爲容器,您應該在攔截器的前後委託您的首選PlatformTransactionManager實施的事務處理。

我已經寫了如何創建前,標有註釋here的方法後,增加了邏輯的攔截器。並演示了您必須在PlatformTransactionManager here上使用哪些方法。

我希望這有助於!