users@glassfish.java.net

JSF Lifecycle objects cannot use CDI?

From: <glassfish_at_javadesktop.org>
Date: Sat, 25 Sep 2010 20:32:32 PDT

I am writing a @FacesComponent annotated backing to a composite component and this works very well. However I need to get to my JPA controllers (most people call these DAOs) and found out that the @Inject annotation doesn't work!

With a little searching I came across this web page: http://dominikdorn.com/2010/04/cdi-weld-manual-bean-lookup/comment-page-1/#comment-845

From this I understand that since the UIComponent is created by the JSF lifecycle, the CDI implementation never sees it so the @Inject annotations are never processed. Can this be correct? Is this a Glassfish problem or a JSF problem?

The workaround works, but it is horrific (the core code from the blog is pasted below). I can't believe this is not a very common roadblock to implementation in JSF and there has to be some better way.

[code]public BeanManager getBeanManager()
    {
        return (BeanManager)
              ((ServletContext) facesContext.getExternalContext().getContext())
                   .getAttribute("javax.enterprise.inject.spi.BeanManager");
    }

public AvailableCountryDao getFacade()
    {
        BeanManager bm = getBeanManager();
        Bean<AvailableCountryDao> bean = (Bean<AvailableCountryDao>) bm.getBeans(AvailableCountryDao.class).iterator().next();
        CreationalContext<AvailableCountryDao> ctx = bm.createCreationalContext(bean);
        AvailableCountryDao dao = (AvailableCountryDao) bm.getReference(bean, AvailableCountryDao.class, ctx); // this could be inlined, but intentionally left this way
        return dao;
    }

[/code]
[Message sent by forum member 'alan94539']

http://forums.java.net/jive/thread.jspa?messageID=483707