Spring中是否有基于@Transactional注释的替代配置?
我想让我的java类尽可能免于Spring,即尽可能与任何框架分离。
答案 0 :(得分:8)
是的,使用aop:config
和tx: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 :(得分:3)
注释是标记应在事务中执行方法的最佳选择。建议用于Spring和EJB 3。
XML方法需要更多配置,不需要重构,您必须在配置中看到某个方法是否会在事务中执行。
由于基于注释的事务支持是大多数开发人员的首选,并且您不喜欢使用Spring的@Transactional
注释,因此我建议您使用自定义注释。
然后你有两个选择:
@Transactional
并在Spring配置中使用<tx:annotation-driven />
元素。这很简单,只需要更新一个注释即可删除Spring依赖项。PlatformTransactionManager
实现。我已经写过如何创建一个拦截器,在一个标有注释here的方法之前和之后添加逻辑。并演示了您必须在PlatformTransactionManager here上使用哪些方法。
我希望这有帮助!