title: Spring Prototype scoped bean in a singleton
body: <p>I am trying to inject a <code>prototype</code> bean in a <code>singleton</code> bean such that every new call to a singleton bean method has a new instance of the prototype bean.</p>
  
  <p>Consider a singleton bean as below:</p>
  
  <pre><code>    @Component
      public class SingletonBean {
         @Autowired 
         private PrototypeBean prototypeBean;
  
         public void doSomething() {
           prototypeBean.setX(1);
           prototypeBean.display();
         }
      }
  </code></pre>
  
  <p>I expect that every time the <code>doSomething()</code> method is called, a new <code>PrototypeBean</code> instance is utilized.</p>
  
  <p>Below is the prototype bean:</p>
  
  <pre><code>     @Component 
       @Scope(value="prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
       public class PrototypeBean {
          Integer x;
  
          void setX(Integer x) {
           this.x = x;
          }
  
          void display() {
            System.out.println(x);
          }
      }
  </code></pre>
  
  <p>What seems to be happening is that spring is being overeager in handing over a new instance of PrototypeBean in the <code>doSomething()</code> method. That is, the 2 lines of code in the <code>doSomething()</code> method are creating a new instance of prototypeBean in each line. </p>
  
  <p>And so in the 2nd line - <code>prototypeBean.display()</code> prints <strong>NULL</strong>.</p>
  
  <p><strong><em>What is missing in configuration for this injection?</em></strong></p>