title: Correct way to get contextual reference of a Java CDI bean
body: <p>I googled for the way to get contextual reference of a CDI bean. I found 2 ways:</p>
  
  <p><strong>1. Using beans.iterator()</strong></p>
  
  <pre><code>    public static &lt;T&gt; T getReference(BeanManager bm, Class&lt;? extends T&gt; type, Annotation... qualifiers) {
          Set&lt;Bean&lt;?&gt;&gt; beans = bm.getBeans(type, qualifiers);
          if (beans == null || beans.isEmpty()) {
              throw new IllegalArgumentException("Bean not found.");
          }
  
          // Using beans.iterator()
          Bean&lt;T&gt; bean = (Bean&lt;T&gt;) beans.iterator().next();
  
          return (T) bm.getReference(bean, type, bm.createCreationalContext(bean));
      }
  </code></pre>
  
  <p><strong>2. Using BeanManager.resolve</strong></p>
  
  <pre><code>    public static &lt;T&gt; T getReference(BeanManager bm, Class&lt;? extends T&gt; type, Annotation... qualifiers) {
          Set&lt;Bean&lt;?&gt;&gt; beans = bm.getBeans(type, qualifiers);
          if (beans == null || beans.isEmpty()) {
              throw new IllegalArgumentException("Bean not found.");
          }
  
          // Using BeanManager.resolve()
          Bean&lt;T&gt; bean = (Bean&lt;T&gt;) bm.resolve(beans);
  
          return (T) bm.getReference(bean, type, bm.createCreationalContext(bean));
      }
  </code></pre>
  
  <p><strong>My question is: Which way is correct? if both are correct, what is context use of each one?</strong></p>
  
  <p>Thank you!</p>