Re: Circular depedency issue - one with constructor arg, one with setter

Connor Barry <[email protected]> Mon, 10 May 2010 10:25:47 -0600
Newsgroups gmane.comp.java.springframework.user
Message-ID <[email protected]>
Not only can I do it in Java, I included the working code in my original 
email. Next time RTFE instead of reacting like a monkey with a yo-yo.

XmlRpcClient client = new XmlRpcClient();
CSPCompatibleTransportFactory tf = new 
CSPCompatibleTransportFactory(client);
client.setTransportFactory(tf);

I expect Spring to do the exact code above, the same way it's able to 
create both beans before calling their setters in a normal 
non-constructor-arg scenario. In case anyone's interested in my 
workaround, I just made a FactoryBean that does the above.

Connor


On 5/10/2010 6:58 AM, Rob Butler wrote:
> Think about it.  Could you do it in Java code?  No.  So why would you expect Spring to be able to do it?
>
> In your example you are passing at least one in a setter.  The same thing should be possible in Spring.
>
> In the Spring config you are trying to construct two objects and pass them to each other simultaneously.  You couldn't do that in Java either.
>
>
> Rob
>
> ----- Original Message ----
>
>    
>> From: Connor Barry<[email protected]>
>> To: List for Spring users<[email protected]>
>> Sent: Sun, May 9, 2010 1:38:15 PM
>> Subject: [Springframework-user] Circular depedency issue - one with constructor arg, one with setter
>>
>> I have two beans, defined as:
>>      
> <bean id="cspTransportFactory"
>
>    
>>
>> class="com.mycompany.CSPCompatibleTransportFactory">
>>      
> <constructor-arg
>    
>> index="0" ref="cspClient" />
>>      
> </bean>
>
> <bean id="cspClient"
>    
>> class="org.apache.xmlrpc.client.XmlRpcClient">
>>      
> <property
>    
>> name="transportFactory" ref="cspTransportFactory"
>> />
>>      
> </bean>
>
> Spring is choking at startup with error:
> Caused
>    
>> by:
>>      
> org.springframework.beans.factory.BeanCurrentlyInCreationException:
>    
>>      
> Error creating bean with name 'cspTransportFactory': Requested bean is
>    
>>      
> currently in creation: Is there an unresolvable circular
>    
>> reference?
>>      
> Previously I was able to construct these beans by injecting
>    
>> instances
>>      
> into one another before calling setter method:
>
> XmlRpcClient
>    
>> client = new XmlRpcClient();
>>      
> CSPCompatibleTransportFactory tf = new
>    
>>      
> CSPCompatibleTransportFactory(client);
> client.setTransportFactory(tf);
>
> Note
>    
>> that I don't have control over the fact that the
>>      
> cspTransportFactory uses
>    
>> constructor injection because I'm extending
>>      
> another API class. Spring can
>    
>> construct a circular dependency in the
>>      
> same matter when two classes use
>    
>> strictly setter injection. Shouldn't it
>>      
> be able to do the same when one of
>    
>> the two classes uses
>> CI?
>>      
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Springframework-user
>    
>> mailing list
>>      
>    
>> href="mailto:[email protected]">[email protected]
>>      
>    
>> href="https://lists.sourceforge.net/lists/listinfo/springframework-user"
>> target=_blank
>>      
>>> https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>        
>
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Springframework-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/springframework-user
>    

------------------------------------------------------------------------------