title: Programmatic lookup/creation of (unmanaged) beans in CDI 1.1
body: <p>I'm currently upgrading our libraries to JavaEE 7 and I have some questions on how to lookup and create (unmanaged) bean instances. I've looked around some other questions here and on the internet and found quite different solutions. I've ignored qualifiers on my examples.</p>
  
  <p>In the old implementation (for CDI 1.0) of the libraries I've only used <em>Lookup #1</em> - even for @Dependent.</p>
  
  <hr>
  
  <p><strong>Lookup #1</strong>: The old way using BeanManager#getBeans()</p>
  
  <pre><code>public static &lt;T&gt; T getBean(Class&lt;T&gt; clazz) {
      Bean&lt;T&gt; bean = (Bean&lt;T&gt;) _beanManager.resolve(_beanManager.getBeans(clazz));
      if(bean.getScope().equals(Dependent.class)) {
          throw new IllegalArgumentException("Cdi#getBean(Class) should only be invoked for classes that do not belong to the "
                  + "@Dependent scope");
      }
      CreationalContext&lt;T&gt; ctx = _beanManager.createCreationalContext(bean);
      return (T) _beanManager.getReference(bean, clazz, ctx);
  }
  </code></pre>
  
  <p>I've added the if-statement because I've read at some other place, that @Dependent scoped beans shouldn't be created on this way (no well-defined lifecycle).</p>
  
  <p><strong>Lookup #2</strong>: The CDI-1.1-way</p>
  
  <pre><code>public static &lt;T&gt; T getBean(Class&lt;T&gt; clazz) {
      return CDI.current().select(clazz).get();
  }
  </code></pre>
  
  <p><strong><em>Q1</strong>: Which one (#1 or #2) should be preferred in CDI 1.1 to perform programmatic lookup of managed beans? Any pros and cons? Other solutions available?</em></p>
  
  <hr>
  
  <p><strong>Dependent / Creation #1</strong>: Use Unmanged and UnmanangedInstance</p>
  
  <pre><code>public static &lt;T&gt; UnmanagedInstance&lt;T&gt; getDependentBean(Class&lt;T&gt; clazz) {
      return new Unmanaged&lt;&gt;(clazz).newInstance();
  }
  </code></pre>
  
  <p>The UnmanagedInstance could be wrapped in a class DependentProvider which calls <code>produce()</code>, <code>inject()</code>, <code>postConstruct()</code> in the constructor and <code>preDestroy()</code>, <code>dispose()</code> in the destroy method (to simplify the usage even more).</p>
  
  <p><strong>Dependent / Creation #2</strong>: Inspired by DeltaSpike</p>
  
  <pre><code>public static &lt;T&gt; DependentProvider&lt;T&gt; getDependentBean(Class&lt;T&gt; clazz) {
      Bean&lt;T&gt; bean = (Bean&lt;T&gt;) _beanManager.resolve(_beanManager.getBeans(clazz));
      CreationalContext&lt;T&gt; ctx = _beanManager.createCreationalContext(bean);
      T instance = (T) _beanManager.getReference(bean, clazz, ctx);
      return new DependentProvider&lt;T&gt;(bean, ctx, instance);
  }
  </code></pre>
  
  <p>The returned provider has a <code>destroy()</code> method, to remove the created instance.</p>
  
  <pre><code>public void destroy() {
      bean.destroy(instance, creationalContext);
  }
  </code></pre>
  
  <p>What I'm missing in the implementation from DeltaSpike is the call to <code>creationalContext.release()</code>. Is this not required?</p>
  
  <p><strong>Creation #3</strong>: Instance => only possible in managed beans</p>
  
  <pre><code>@ViewScoped
  class X implements Serializable {
      @Inject Instance&lt;MyObject&gt; _myObjectProvider;
  
      public void doSomething() {
          MyObject obj = _myObjectProvider.get();
          // ... work with obj
  
          // call _myObjectProvider.destroy(obj) as soon as we're done with the instance of MyObject.
      }
  }
  </code></pre>
  
  <p><strong><em>Q2</strong>: Again, which one (#1 or #2, #3) should be preferred for programmatic creation of unmanaged "beans"? Any pros and cons? Other solutions available?</em></p>
  
  <hr>
  
  <p>Finally I have a question on how to check if a bean is already instantiated (e.g. a session scoped bean) or not. I came up with the following method (for non-@Dependant only):</p>
  
  <pre><code>public &lt;T&gt; boolean existsBeanInstance(String name) {
      Bean&lt;T&gt; bean = (Bean&lt;T&gt;) _beanManager.resolve(_beanManager.getBeans(name));
      Context beanScopeContext = _beanManager.getContext(bean.getScope());
      return beanScopeContext.get(bean) != null;
  }
  </code></pre>
  
  <p><strong><em>Q3</strong>: Is this reliable?</em></p>