PDA

View Full Version : Bean initialization order problem



mfranken
Sep 29th, 2004, 06:12 PM
I have a bean "Master" and a bean "Detail". The Detail has a reference to Master, and vice versa. Additionally the Detail bean has a init method that reads and uses a value from the Master (which provides the default value) to set one of its own attributes.

When I wire a master and two detail beans from the applicationContext.xml I'm not getting the behavior that I'd expect as the first detail does contain the proper value for the attribute set by the init method, where the second does not.

I've seen that AbstractAutowireCapableBeanFactory.createBean() creates a bean with a reference, first the refenence is resolved, and then the attributes are set, and then the init method is called.

This results in the following:
detail1 is created
a reference to master is resolved
master is created
a reference to detail1 resolved
a reference to detail2 is resolved
detail2 is created
a reference to master is resolved
detail2 attributes are set
detail2 init method is called, while master's attributes are not set yet (resulting in wrong value set for detail2)
master attribues are set
detail1 attributes are set
detail1 init method is called (resulting in proper value)

So should the implementation of AbstractAutowireCapableBeanFactory.createBean() be changed to set attributes first at all times (which doesn't hurt I'd guess), then resolve the references, and then call init? Or is this intended behavior and should I wire my beans differently.

cheers,

Michael Franken

Henri
Nov 11th, 2004, 07:48 AM
Hi Michael,

I experienced exactly the same problem and was VERY disappointed :cry:, because IMHO:
1. This could be solved easily by setting all properties of the dependent beans first, and
2. There is no answer to your post!

It took me an hour to figure it out, but I found a solution. Just register your bean as an ApplicationListener and perform your initialization in response to the ContextRefreshedEvent.

Regards, Henri

mfranken
Dec 8th, 2004, 03:36 AM
Hi Henri,

I was disappointed too, but have found a simple work around, that actually does not depend on Spring to resolve the circular dependency.
I'll be watching this thing.

regards,

Michael

Henri
Dec 8th, 2004, 04:06 AM
Michael,

Would you mind sharing your workaround with us?

Thanks, Henri

mfranken
Dec 8th, 2004, 04:12 AM
Hi Henri,

I've given my detail bean (Collection) the responsibility to add it self to its Master (CollectionManager):


/**
* Set the collectionManager, <b>and</b> add itself to the collectionManager's collections.
*
* @param thisManager The CollectionManager holding this collection.
*/
public final void setManager&#40;final CollectionManager thisManager&#41; &#123;
this.manager = thisManager;
thisManager.addCollection&#40;this&#41;;
&#125;


The resulting bean wiring is then simple, only the detail has to refer to the master


<property name="manager">
<ref local="collectionMan"/>
</property>