title: Howto generate prototype objects with in a singleton bean using spring java configurations
body: <p>Here is what i have right now which works fine. All it does is a market class which returns an array of items objects:</p>
  
  <p>I have class market </p>
  
  <pre><code>class market {
  
      public ArrayList&lt;Items&gt; createItems(HashMap&lt;String,String&gt; map) {
          ArrayList&lt;Items&gt; array = new ArrayList&lt;Items&gt;();
          for (Map.Entry&lt;String, String&gt; m : map.entrySet()) {
              Item item = new Item();
              item.setName(m.key());
              item.setValue(m.value());
              array.add(item);
          }
          return array;
      }
  }
  </code></pre>
  
  <p>class Item is simple class with getter and setter for name and value</p>
  
  <p>Here is how my config file looks:</p>
  
  <pre><code>@Configuration
  public class MarketConfig {
  
      @Bean
      public Market market() {
          return new Market();
      }
  }
  </code></pre>
  
  <p>How I want to change my code:( REASON: i dont want </p>
  
  <pre><code>Item item = new Item(); 
  </code></pre>
  
  <p>in then method. I want Spring to inject it into Market)</p>
  
  <pre><code>class market {
  
      public Item item;
      //getters and setters for item
  
      public ArrayList&lt;Items&gt; createItems(HashMap&lt;String,String&gt; map) {
          ArrayList&lt;Items&gt; array = new ArrayList&lt;Items&gt;();
          for (Map.Entry&lt;String, String&gt; m : map.entrySet()) {
               item.setName(m.key());
               item.setValue(m.value());
               array.add(item);
          }
          return array;
      }
  }
  
  @Configuration
  public class MarketConfig {
  
      @Bean
      @Scope("prototype")
      public Item item() {
          return new Item();
      }
  
      @Bean
      public Market market() {
          Market bean = new Market();
          bean.setItem(item());
      }
  }
  </code></pre>
  
  <p>I know that prototype scope will give me new bean each time i call item();
   Now i want new bean for each iteration in the for loop of createItems method. How can i tell spring to give me. </p>
  
  <p>One way i know is Do</p>
  
  <pre><code>applicationContext context = new AnnotationConfigApplicationContext();
  context.getBean(Item.class);
  </code></pre>
  
  <p>But is there any other way to get my work done. 
  Thanks</p>