[weld-dev] BeanManager.getReference() VS Context.get()

Muhammad Bhutto muhammad.bhutto at gmail.com
Tue Nov 19 05:51:53 EST 2013


Hi Jozef,

Thank you very much for prompt reply.

Thanks again.

Regards,
Muhammad Bhutto


On Tue, Nov 19, 2013 at 2:51 PM, Jozef Hartinger <jharting at redhat.com>wrote:

>  On 11/19/2013 03:09 AM, Muhammad Bhutto wrote:
>
>  Hi All,
>
>  Can you please explain me this one, I have confusion which one is better.
>
>  1.
>
>  Bean<MyBean> bean = (Bean<MyBean>)
> beanManager.resolve(beanManager.getBeans(MyBean.class));
> MyBean= (MyBean) beanManager.getReference(bean, bean.getBeanClass(),
> beanManager.createCreationalContext(bean));
>
> 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.
>
>
>  2.
>
>  Bean<MyBean> bean = (Bean<MyBean>)
> beanManager.resolve(beanManager.getBeans(MyBean.class));
> MyBean bean = beanManager.getContext(bean.getScope()).get(bean,
> beanManager.createCreationalContext(bean));
>
> 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.
>
> 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).
>
>
>
>  As i know  BeanManager.getReference() always creates a whole new proxy
> instance, while the Context.get() reuses an existing proxy instance if
> already created before.
>
>   Is BeanManager.getReference() is more use full than  Context.get() ??
>
>  Thanks
>
>  Muhammad Asif Bhutto
>
>
> _______________________________________________
> weld-dev mailing listweld-dev at lists.jboss.orghttps://lists.jboss.org/mailman/listinfo/weld-dev
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/weld-dev/attachments/20131119/4eb0e990/attachment-0001.html 


More information about the weld-dev mailing list