title: How to avoid using ApplicationContext.getBean() when implementing Spring IOC
body: <p>I'm just getting started with Spring IOC concept. I often see most of the examples found in the internet use the code to get the object.</p>
  
  <pre><code>ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  Hello hello = (Hello) appContext.getBean("hello"); 
  </code></pre>
  
  <p>As a reference from these questions <a href="https://stackoverflow.com/questions/812415/why-is-springs-applicationcontext-getbean-considered-bad">1</a> and <a href="https://stackoverflow.com/questions/9675130/using-spring-in-standalone-apps">2</a> in the stackoverflow. I've inferred that, it's not necessary to use appContext.getBean("hello") in the code which is considered to be the bad practice. Also, not recommended anymore. Correct me right here, If my inference is wrong. </p>
  
  <p>Keeping that in view, I have made changes in my project accordingly.
  Here's my applicationContext.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"&gt;
  &lt;bean id="utilClassRef" class="org.hd.derbyops.DUtils" lazy-init="false" /&gt;
  &lt;bean id="appContext" class="org.hd.derbyops.ContextProvider" lazy-init="false"&gt;
     &lt;property name="utils" ref="utilClassRef" /&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My contextProvider Class Code</p>
  
  <pre><code>public class ContextProvider implements ApplicationContextAware {
  
      private static ApplicationContext ctx;
  
      /**
       * Objects as properties
       */
      private static DUtils utils;
  
      public void setApplicationContext(ApplicationContext appContext)
              throws BeansException {
          ctx = appContext;
  
      }
  
      public static ApplicationContext getApplicationContext() {
          return ctx;
      }
  
      public static DUtils getUtils() {
          return utils;
      }
  
      public void setUtils(DUtils dUtilsRef) {
          utils = dUtilsRef;
      }
  
  }
  </code></pre>
  
  <p>For instance, consider a class A that depends on org.hd.derbyops.DUtils. 
  I'm using the following code line</p>
  
  <pre><code>ContextProvider.getUtils();
  </code></pre>
  
  <p>inorder to get DUtils Object in class A, thus avoiding usage of  <code>ApplicationContext.getBean()</code> anywhere in my code.</p>
  
  <p>Assume, if I have 10 classes and my class A is dependent on all of them, whose objects to be created and accessed without using <code>ApplicationContext.getBean()</code>. In that case also, as done above, I have a thought of creating properties of ContextProvider class followed by setter's and getter's of that property, where in <code>get&lt;PropertyName&gt;</code> is static. So that, I can use it wherever I'm in need of an object, like this</p>
  
  <pre><code>ContextProvider.get&lt;PropertyName&gt;;
  </code></pre>
  
  <p>Here's my brief question. 
  Firstly, Is my approach right? If it's right, loading all the beans at the start-up, wouldn't it be a performance killer? How would you do that in your applications without calling getBean atleast more than once?</p>
  
  <p>If you were to design a web-application &amp; you were to implement Spring IOC, without using <code>ApplicationContext.getBean()</code> in any of the code. How would you do that? </p>
  
  <p><strong>Note:</strong> with reference to the other questions tagged above </p>
  
  <blockquote>
    <p>Calling ApplicationContext.getBean() is not Inversion of Control!</p>
  </blockquote>