<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:8pt">In other words:<br><br>.) BeanManager#getReference will return you a 'Contextual Reference', means a normalscoping proxy for the bean. If you have a @SessionScoped User usr; then the Contextual Reference usr&nbsp; will 'point' to the respective User instance (the 'Contextual Instance') of the _current_ session for each invocation. Two different invocations to usr.getName() from 2 different web browsers will give you different answers.<br><br>.) Contest#get() will return you the internal 'Contextual Instance' without the normalscoping proxy. This is usually nothing a user should call himself. If you get&nbsp; the User usr for "Karl" that way and store it in an @ApplicationScoped bean or in a static variable, then it will _always_ remain to be the user "Karl" - even for web requests from other
 browsers! You will get a direct, non-proxied instance.<br><br>LieGrue,<br>strub<br><div><span><br></span></div><div><br></div><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;">  <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 8pt;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, Sans-Serif; font-size: 12pt;"> <div dir="ltr"> <hr size="1">  <font face="Arial" size="2"> <b><span style="font-weight:bold;">From:</span></b> Jozef Hartinger &lt;jharting@redhat.com&gt;<br> <b><span style="font-weight: bold;">To:</span></b> Muhammad Bhutto &lt;muhammad.bhutto@gmail.com&gt; <br><b><span style="font-weight: bold;">Cc:</span></b> weld-dev@lists.jboss.org <br> <b><span style="font-weight: bold;">Sent:</span></b> Tuesday, 19 November 2013, 10:51<br> <b><span style="font-weight:
 bold;">Subject:</span></b> Re: [weld-dev] BeanManager.getReference() VS Context.get()<br> </font> </div> <div class="y_msg_container"><br><div id="yiv4041003502"><div>
    <div class="yiv4041003502moz-cite-prefix">On 11/19/2013 03:09 AM, Muhammad Bhutto
      wrote:<br clear="none">
    </div>
    <blockquote type="cite">
      <div dir="ltr">
        <div>Hi All,</div>
        <div><br clear="none">
        </div>
        <div>Can you please explain me this one, I have confusion which
          one is better.</div>
        <div><br clear="none">
        </div>
        <div>1.<br clear="none">
        </div>
        <div><br clear="none">
        </div>
        <div>
          <div>Bean&lt;MyBean&gt; bean = (Bean&lt;MyBean&gt;)
            beanManager.resolve(beanManager.getBeans(MyBean.class));</div>
          <div>MyBean= (MyBean) beanManager.getReference(bean,
            bean.getBeanClass(),
            beanManager.createCreationalContext(bean));</div>
        </div>
      </div>
    </blockquote>
    This one gives you a new instance of a client proxy. The client
    proxy will forward method calls to the current contextual instance
    of a particular context. You can therefore obtain the proxy once and
    keep it and the method calls will be invoked on the current instance
    (e.g. current request). It is also useful if the contextual instance
    is not serializable - the client proxy will be and will reconnect
    after you deserialize it.<br clear="none">
    <blockquote type="cite">
      <div dir="ltr">
        <div><br clear="none">
        </div>
        <div>2.</div>
        <div><br clear="none">
        </div>
        <div>
          <div>Bean&lt;MyBean&gt; bean = (Bean&lt;MyBean&gt;)
            beanManager.resolve(beanManager.getBeans(MyBean.class));</div>
          <div>MyBean bean =
            beanManager.getContext(bean.getScope()).get(bean,
            beanManager.createCreationalContext(bean));</div>
        </div>
      </div>
    </blockquote>
    This obtains the target instance without a client proxy. You may
    still see a Weld's proxy in the class name but that is an enhanced
    subclass that provides interception and decoration. If the bean is
    not intercepted nor decorated this will be a plain instance of the
    given bean.<br clear="none">
    <br clear="none">
    Usually (1) is more suitable unless you have a special use-case
    where you need to access the target instance directly (e.g. to
    access its fields).<div class="yiv4041003502yqt9049636189" id="yiv4041003502yqtfd51604"><br clear="none">
    <blockquote type="cite">
      <div dir="ltr">
        <div><br clear="none">
        </div>
        <div><br clear="none">
        </div>
        <div>As i know &nbsp;BeanManager.getReference() always creates a
          whole new proxy instance, while the Context.get() reuses an
          existing proxy instance if already created before.<br clear="none">
        </div>
        <div><br clear="none">
        </div>
        <div>&nbsp;Is&nbsp;BeanManager.getReference() is more use full than
          &nbsp;Context.get() ??<br clear="none">
        </div>
        <div><br clear="none">
        </div>
        <div>Thanks</div>
        <div><br clear="none">
        </div>
        <div>Muhammad Asif Bhutto</div>
      </div>
      <br clear="none">
      <fieldset class="yiv4041003502mimeAttachmentHeader"></fieldset>
      <br clear="none">
      <pre>_______________________________________________
weld-dev mailing list
<a rel="nofollow" shape="rect" class="yiv4041003502moz-txt-link-abbreviated" ymailto="mailto:weld-dev@lists.jboss.org" target="_blank" href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>
<a rel="nofollow" shape="rect" class="yiv4041003502moz-txt-link-freetext" target="_blank" href="https://lists.jboss.org/mailman/listinfo/weld-dev">https://lists.jboss.org/mailman/listinfo/weld-dev</a></pre>
    </blockquote>
    <br clear="none">
  </div></div></div><br><div class="yqt9049636189" id="yqtfd76834">_______________________________________________<br clear="none">weld-dev mailing list<br clear="none"><a shape="rect" ymailto="mailto:weld-dev@lists.jboss.org" href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br clear="none"><a shape="rect" href="https://lists.jboss.org/mailman/listinfo/weld-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/weld-dev</a></div><br><br></div> </div> </div> </blockquote><div></div>   </div></body></html>