Nate's Programming Blog

I discovered a work around for being able to use declarative transactions with classes that don’t implement an interface. Basically this is one of the ways to configure a base AOP transaction proxy using Spring.


<bean id=”txProxyTemplate” abstract=”true”
class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean”>
<property name=”transactionManager”><ref bean=”transactionManager”/></property>
<property name=”transactionAttributes”>
<props>
<prop key=”*”>PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>


<bean id=”petStore” parent=”txProxyTemplate”>
<property name=”target”>
<bean class=”my.example.transaction.class.PetStore” />
</property>
</bean>

There are some other things you might want to do here, but this is the basic idea. Now lets say that PetStore doesn’t implement an interface. If you try and inject this as a dependency in another class, you will get an error. It will try and inject the parent proxy class instead and you will either not find your dependency or you will be a class cast exception. In order to get around this you can add CGLIB to your classpath and add this property to your petStore bean.


<property name=”proxyTargetClass”><value>true</value></property>

This will allow you to proxy the target class and not it’s interfaces. Thank you James for helping me discover this.


Name (required)

Email (required)

Website

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Feel free to leave a comment

top