title: How to instantiate Spring managed beans at runtime?
body: <p>I stuck with a simple refactoring from plain Java to Spring. Application has a &quot;Container&quot; object which instantiates its parts at runtime. Let me explain with the code:</p>
  <pre><code>public class Container {
      private List&lt;RuntimeBean&gt; runtimeBeans = new ArrayList&lt;RuntimeBean&gt;();
  
      public void load() {
          // repeated several times depending on external data/environment
          RuntimeBean beanRuntime = createRuntimeBean();
          runtimeBeans.add(beanRuntime);
      }
  
      public RuntimeBean createRuntimeBean() {
           // should create bean which internally can have some 
           // spring annotations or in other words
           // should be managed by spring
      }
  }
  </code></pre>
  <p>Basically, during load container asks some external system to provide him information about number and configuration of each <code>RuntimeBean</code> and then it create beans according to given spec.</p>
  <p>The problem is: usually when we do in Spring</p>
  <pre><code>ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
  Container container = (Container) context.getBean(&quot;container&quot;);
  </code></pre>
  <p>our object is fully configured and have all dependencies injected. But in my case I have to instantiate some objects which also needs dependency injection after I execute load() method.<br />
  How can I achieve that?</p>
  <p>I am using a Java-based config. I already tried making a factory for <code>RuntimeBeans</code>:</p>
  <pre><code>public class BeanRuntimeFactory {
  
      @Bean
      public RuntimeBean createRuntimeBean() {
          return new RuntimeBean();
      }
  }
  </code></pre>
  <p>Expecting <code>@Bean</code> to work in so called 'lite' mode. <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html" rel="noreferrer">http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html</a> Unfortunately, I found no difference with simply doing new RuntimeBean();
  Here is a post with a similar issue: <a href="https://stackoverflow.com/questions/4970297/how-to-get-beans-created-by-factorybean-spring-managed">How to get beans created by FactoryBean spring managed?</a></p>
  <p>There is also <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Configurable.html" rel="noreferrer">http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Configurable.html</a> but it looks like a hammer in my case.</p>
  <p>I also tried ApplicationContext.getBean(&quot;runtimeBean&quot;, args) where runtimeBean has a &quot;Prototype&quot; scope, but getBean is an awful solution.</p>
  <hr />
  <h2>Update 1</h2>
  <p>To be more concrete I am trying to refactor this class:
  <a href="https://github.com/apache/lucene-solr/blob/trunk/solr/core/src/java/org/apache/solr/core/CoreContainer.java" rel="noreferrer">https://github.com/apache/lucene-solr/blob/trunk/solr/core/src/java/org/apache/solr/core/CoreContainer.java</a>
  @see #load() method and find &quot;return create(cd, false);&quot;</p>
  <h2>Update 2</h2>
  <p>I found quite interesting thing called &quot;lookup method injection&quot; in spring documentation:
  <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection" rel="noreferrer">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection</a></p>
  <p>And also an interesting jira ticket <a href="https://jira.spring.io/browse/SPR-5192" rel="noreferrer">https://jira.spring.io/browse/SPR-5192</a> where Phil Webb says <a href="https://jira.spring.io/browse/SPR-5192?focusedCommentId=86051&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-86051" rel="noreferrer">https://jira.spring.io/browse/SPR-5192?focusedCommentId=86051&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-86051</a> that javax.inject.Provider should be used here (it reminds me Guice).</p>
  <h2>Update 3</h2>
  <p>There is also <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.html" rel="noreferrer">http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.html</a></p>
  <h2>Update 4</h2>
  <p>The issue with all these 'lookup' methods is they don't support passing any arguments.. I also need to pass arguments as I would do with applicationContext.getBean(&quot;runtimeBean&quot;, arg1, arg2). Looks like it was fixed at some point with <a href="https://jira.spring.io/browse/SPR-7431" rel="noreferrer">https://jira.spring.io/browse/SPR-7431</a></p>
  <h2>Update 5</h2>
  <p>Google Guice have a neat feature for it called AssistedInject. <a href="https://github.com/google/guice/wiki/AssistedInject" rel="noreferrer">https://github.com/google/guice/wiki/AssistedInject</a></p>