Getting UIScoped Beans from BeanContainer - Vaadin CDI

Hi, I’m trying to get an UIScoped bean from a non-managed bean by using the standard BeanContainer.
But I’m not getting the correct bean instance (from the same UI)

I’m using this code to get the bean:

private <T> T lookup(Class<T> clazz) throws NamingException {
    Bean bean = (beanManager.getBeans(clazz).iterator().next());
    CreationalContext<T> creationalContext = beanManager.createCreationalContext(bean);
    return (T) beanManager.getReference(bean, clazz, creationalContext); // this could be inlined, but intentionally left this way
}

This works well usually, but it does not seem to work when using it for @UIScope beans.
Shuld I use some other code to get the correct instance or am I doing something by mistake?

Thanks
Regards

An old thread, but at least a partial explanation: Vaadin CDI 1.0.0.alpha2 and earlier use UI.getCurrent() to find the UI for the bean. This helps find the UI in background threads etc. using a ThreadLocal managed by CurrentInstance which is updated by Vaadin for request processing and UI.access(). However, this approach causes other problems in some situations such as your case.

The recently released 1.0.0.alpha3 and the upcoming beta1 try to change this to use a more CDI style approach, but this also has its complications because of the proxies generated by CDI implementations (at least Apache OpenWebBeans) for @NormalScoped beans, for CDI events, for request processing phases before and after the UI specific phases etc. I hope we’ll be able to resolve these.

Hi Henri,

I updated to beta 3 and ran into this issue: I have a @UIScoped component derived from a Vaadin component and inject it into several CDIViews. It used to keep its state when navigating between the different views (as per customer requirement). Now - after the update - it doesn’t any more. Changing the scope to “@NormalUIScoped” didn’t help either. During deployment I get the message “A Vaadin Component cannot be a proxy”. I am aware of the problems when using “.equals” on proxies and real objects etc. but is there really absolutely no way to proxy a vaadin component any more? Will it remain this way in future versions of the CDI add-on?

Thanks,
Jürgen