In other words:

.) 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  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.

.) 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  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.


From: Jozef Hartinger <>
To: Muhammad Bhutto <>
Sent: Tuesday, 19 November 2013, 10:51
Subject: Re: [weld-dev] BeanManager.getReference() VS Context.get()

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.


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.


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() ??


Muhammad Asif Bhutto

weld-dev mailing list

weld-dev mailing list