Another ColdSpring Gotcha

Found another gotcha with ColdSpring yesterday.  Chris confirmed it as a bug on the mailing list, but thought I'd share here as well.  If you have a circular dependancy between two proxied beans, the dependancy is only "half" resolved.  That is, one bean will get a correctly proxied object injected, but the other will get an uninitialized ProxyFactoryBean instance.  Here's an example:

<bean id="imageservice" class="coldspring.aop.framework.ProxyFactoryBean">
<property name="target">
<bean id="imageserviceTarget" class="sorter.cfcs.imageservice">
<property name="recipientservice"><ref bean="recipientservice" /></property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>transactionAdvice</value>
</list>
</property>
</bean>

<bean id="recipientservice" class="coldspring.aop.framework.ProxyFactoryBean">
<property name="target">
<bean id="recipientserviceTarget" class="sorter.cfcs.recipientservice">
<property name="imageservice"><ref bean="imageservice" /></property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>transactionAdvice</value>
</list>
</property>
</bean>

What's the solution?  You have to avoid the circular dependancy through the proxies.  You could have half the dependancy reference the non-proxied (target) bean directly, but then you lose whatever aspects the proxy is applying.  The better solution is to make both beans BeanFactoryAware (by adding a setBeanFactory method that accepts an object of type coldspring.beans.BeanFactory), and then define and configure an init-method for both objects and manually get the dependancies from the bean factory in the configure method.  Not an ideal solution, obviously, but it does allow you to use the applied aspects.

Comments are closed.