title: Circular dependency in Spring By setter injection
body: <p>Consider the following code:<br/>
  Here is class A</p>
  
  <pre><code>public class A {
  
      public A() {
          System.out.println("Creating instance of A");
      }
  
      private B b;
  
      public void setB(B b) {
          System.out.println("Setting property b of A instance");
          this.b = b;
      }
  
  }
  </code></pre>
  
  <p>Here is class B   </p>
  
  <pre><code>public class B {
  
      public B() {
          System.out.println("Creating instance of B");
      }
  
      private A a;
  
      public void setA(A a) {
          System.out.println("Setting property a of B instance");
          this.a = a;
      }
  
  }
  </code></pre>
  
  <p>We have this configuration file:</p>
  
  <pre><code>&lt;bean id="a" class="mypackage.A"&gt;
      &lt;property name="b" ref="b" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="b" class="mypackage.B"&gt;
      &lt;property name="a" ref="a" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Creating instance of A
  Creating instance of B
  Setting property a of B instance
  Setting property b of A instance
  </code></pre>
  
  <p>when <code>a</code> is injected into <code>b</code>, <code>a</code> is not yet fully initialized.</p>
  
  <p>My question:<br/>
  Even though 'a' is not fully initialized when 'b' gets reference to 'a' , but after few moments 'a' will be completely initialized. Since 'b' has a reference to 'a', then 'b' will be pointing to fully initialized 'a' after few moments. 
  This explanation is generally given to show disadvantage of circular dependency.
  But in the end we have a normal situation of both fully initialized beans 'a' and 'b' pointing to each other. Hence, how does this example show us disadvantage of using circular dependency ?</p>
  
  <p>I would quote from spring docs:</p>
  
  <blockquote>
    <p>Unlike the typical case (with no circular dependencies), a circular
    dependency between bean A and bean B forces one of the beans to be
    injected into the other prior to being fully initialized itself (a
    classic chicken/egg scenario)</p>
  </blockquote>
  
  <p>Reference : Example taken from <a href="https://stackoverflow.com/questions/3485347/circular-dependency-in-spring">Circular dependency in spring</a></p>