开发者

Safeguard against forgetting to configure transactions in spring

开发者 https://www.devze.com 2022-12-28 04:36 出处:网络
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,

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.

0

精彩评论

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

关注公众号