Safeguard against forgetting to configure transactions in spring
Is there a way to make beans unavailable in spring framework? i would like to ensure that in case a developer forgot开发者_StackOverflow中文版 to wrap transaction around a service in spring framework, that service should become unavailable at run time. Thank you,
You can do something like this, with AOP. But instead, you can add automatic transactions to every service method with AOP:
<!-- proxy-target-class should be true, if you don't use interfaces -->
<aop:config proxy-target-class="false">
<aop:pointcut id="serviceMethods"
expression="execution(* com.yourproject.services..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="yourTransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
You can override these default settings by using @Transactional
. However, be advices that the precedence is not straightforward. Check this article about it.
精彩评论