Thursday, November 19, 2015

RequestFactory Services asJava EE managed Beans

after experimenting with subclassing RequestFactoryServlet for a while, I devised a new trick to have RequestFactory Services be managed Beans.
It is a simple trick where a ServiceLocator obtains Services from the BeanManager.
I'd like to share it with the community.

-- x --
public final class BeanManagerServiceLocator implements ServiceLocator
    @Override public Object getInstance (Class<?> clazz)
        return getReference(clazz);
    public static <T> T getReference (Class<T> clazz)
            InitialContext context = new InitialContext();
            BeanManager beanManager = (BeanManager) context.lookup("java:comp/BeanManager");
            Set<Bean<?>> beans = beanManager.getBeans(clazz);
            Bean<?> bean = beanManager.resolve(beans);
            CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
            Object object = beanManager.getReference(bean, bean.getBeanClass(), creationalContext);
            return clazz.cast(object);
        catch (NamingException e)
            throw new RuntimeException(e);
-- x --


You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment