Announcement Announcement Module
Collapse
No announcement yet.
Bean initialization order problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean initialization order problem

    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

  • #2
    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

    Comment


    • #3
      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

      Comment


      • #4
        Michael,

        Would you mind sharing your workaround with us?

        Thanks, Henri

        Comment


        • #5
          Hi Henri,

          I've given my detail bean (Collection) the responsibility to add it self to its Master (CollectionManager):
          Code:
              /**
               * 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
          Code:
                  <property name="manager">
                      <ref local="collectionMan"/>
                  </property>

          Comment

          Working...
          X