Issue Details (XML | Word | Printable)

Key: SPR-4566
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Juergen Hoeller
Reporter: Morten Andersen-Gott
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Spring Framework

BeanCurrentlyInCreationException is thrown when there are circular references between aop-proxied references

Created: 11/Mar/08 06:38 AM   Updated: 13/Mar/08 06:02 AM
Component/s: SpringAOP
Affects Version/s: 2.0.7, 2.5.2
Fix Version/s: 2.5.3

Time Tracking:
Not Specified

File Attachments: 1. Zip Archive beanInCreationException.zip (2 kB)

Environment: Win XP

Virtual Machine: Sun JVM - 1.5
Platform: Standalone


 Description  « Hide
Using the applicationContext below, BeanCurrentlyInCreationException is thrown on application startup. If I remove the aop-config element, the application starts fine and I am able to execute both services.

The use of aop on both components seems perfectly legit to me, especially if the aspects are performing logging. The issue has been raised in several threads in the forum as well:
http://forum.springframework.org/showthread.php?p=168972
http://forum.springframework.org/showthread.php?t=45411

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="serviceA" class="com.test.ServiceAImpl">
<property name="serviceB" ref="serviceB"/>
</bean>

<bean id="serviceB" class="com.test.ServiceBImpl">
<property name="serviceA" ref="serviceA"/>
</bean>

<bean id="traceInterceptor" class="com.test.TraceInterceptor">
</bean>

<aop:config>
<aop:pointcut id="allServiceMethods" expression="execution(* com.test..*.*(..))"/>
<aop:advisor advice-ref="traceInterceptor" pointcut-ref="allServiceMethods" />
</aop:config>

</beans>

 All   Comments   Work Log   Change History   FishEye   Builds      Sort Order: Ascending order - Click to sort in descending order
Morten Andersen-Gott added a comment - 11/Mar/08 06:45 AM
Complete source code that results in the exception. Requires Spring and aspectj.

Morten Andersen-Gott added a comment - 11/Mar/08 07:14 AM
Replacing the bean definitions in the description with the defnitions below, using the ProxyBeanFactory gives me the desired functionality, but with excess xml configuration.

<bean id="serviceATarget" class="com.test.ServiceAImpl">
<property name="serviceB" ref="serviceB"/>
</bean>

<bean id="serviceBTarget" class="com.test.ServiceBImpl">
<property name="serviceA" ref="serviceA"/>
</bean>

<bean id="serviceA"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>com.test.ServiceA</value></property>
<property name="target"><ref local="serviceATarget"/></property>
<property name="interceptorNames">
<list>
<value>traceInterceptor</value>
</list>
</property>
</bean>

<bean id="serviceB"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces"><value>com.test.ServiceB</value></property>
<property name="target"><ref local="serviceBTarget"/></property>
<property name="interceptorNames">
<list>
<value>traceInterceptor</value>
</list>
</property>
</bean>

<bean id="traceInterceptor" class="com.test.TraceInterceptor">
</bean>

Juergen Hoeller added a comment - 11/Mar/08 04:02 PM
This is actually a known limitation of auto-proxying, not really a bug. The ProxyFactoryBean model uses different semantics (with the target instance being available for early access a a dedicated bean, in contrast to auto-proxying) which is why you can make it work in your case.

That said, I agree that it would be nice to make this work. I have prototyped a getEarlyBeanReference mechanism for our AutoProxyCreator that allows to obtain a proxy reference early (i.e. before the target instance is fully configured). This works fine already; I'll do some further testing before committing it later this week.

Juergen

Juergen Hoeller added a comment - 13/Mar/08 06:02 AM
This is committed now for inclusion in 2.5.3. The mechanism is relatively straightforward, so should work for any such scenario - with the usual general drawbacks of circular references, of course.

This will be available in tonight's 2.5.3 snapshot (http://static.springframework.org/downloads/nightly/snapshot-download.php?project=SPR) already. Feel free to give it a try...

Juergen