title: Spring IoC - Ensuring all beans are created before @PostConstruct/afterProperiesSet
body: <p>I've got a project using Spring IoC for dependency injection, and I'm trying to slightly simplify my model by having one central location for accessing most of my beans. </p>
  
  <p>I'm running into a problem using the <code>@PostConstruct</code> mechanism, or alternatively when implementing the <code>InitializingBean</code> interface. While all the dependencies in that particular bean may have been resolved, the dependencies in injected beans may not have been. For example I have:</p>
  
  <pre><code>public class A {
      public void doSomething() {};
  }
  
  public class B {
      private A objectA;
  
      @Required
      public void setObjectA(A objectA) {
          this.objectA = objectA;
      }
  
      public A getObjectA() {
          return objectA;
      }
  }
  
  public class C implements InitializingBean {
      private B objectB;
  
      @Required
      public void setObjectB(B objectB) {
          this.objectB = objectB;
      }
  
      public void afterPropertiesSet() throws Exception {
          objectB.getObjectA().doSomething();
      }
  }
  </code></pre>
  
  <p>My context.xml file defines these three beans and injects the appropriate properties, but I get a <code>NullPointerException</code> when the object of class C gets instantiated and the <code>afterPropertiesSet</code> method is called, debugging shows me that the call to <code>getObjectA()</code> returns <code>null</code>. If I inject object A directly into class C I get no errors.</p>
  
  <p>Is there a mechanism I can use to ensure that all beans have been completely instantiated before my <code>afterPropertiesSet</code> method / any method annotated with <code>@PostConstruct</code> is called?</p>
  
  <p>Thanks,</p>
  
  <p>Joseph.</p>