title: How to handle cyclic dependency in spring
body: <p>For example i have two beans:</p>
  
  <pre><code>class Bean1 {
    private SomeService service1;
    private SomeService servive2;
    private Bean2 bean2;
  
    public void doStuff() {
       service1.doActualStuff();
    }
  
    public void setBean2(Bean2 bean2) {
       this.bean2 = bean2;
    }
  
    public Bean2 getBean2() { return this.bean2 }
  }
  
  class Bean2 {
     private Bean1 bean1;
     private SomeService3 service3;
  
     public void init() {
         bean1.doStuff();
     }
  
     public void setBean1(Bean1 bean1) {
        this.bean1 = bean1;
     }
  
  }
  </code></pre>
  
  <p>Now if i try to configure them in spring in the folllowing way:</p>
  
  <pre><code>&lt;bean id="service1" class="SomeService"&gt;
      ...
  &lt;/bean&gt;
  &lt;bean id="bean1" class="Bean1"&gt;
     &lt;property name="bean2" ref="bean2"/&gt;
     &lt;property name="service1" ref="service1"/&gt;
     ...
  &lt;/bean&gt;
  &lt;bean id="bean2" class="Bean2" init-method="init"&gt;
     &lt;property name="bean1" ref="bean1"/&gt;
     ...
  &lt;/bean&gt;
  </code></pre>
  
  <p>Init method of bean2 is executed. Bean2 has injected bean1, but bean1 itself is not fully initialized so calling bean1.doStuff() that will call service1.doActualStuff() will return into NPE. Why bean1 is not fully initialized?</p>