tags:

views:

19

answers:

1

Is there a way to make beans unavailable in spring framework? i would like to ensure that in case a developer forgot to wrap transaction around a service in spring framework, that service should become unavailable at run time. Thank you,

+1  A: 

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.

Bozho