users@glassfish.java.net

Re: CDI injection broken between POJOs

From: <glassfish_at_javadesktop.org>
Date: Fri, 09 Apr 2010 17:05:14 PDT

Thanks for the quick reply, Steve.

> Your POJO isn't container managed, so you can't use
> CDI like you can in an
> EJB. The EJB container is what does the CDI magic
> for your EJBs.

There seems to be a common misconception that CDI only works for traditional container-managed components (EJBs, servlets, etc.). But I'm pretty sure that's not the case.

Here's a definition of managed beans from the JEE 6 Tutorial:

http://java.sun.com/javaee/6/docs/tutorial/doc/gjfzi.html

Here's an example of one POJO being injected into another POJO:

http://java.sun.com/javaee/6/docs/tutorial/doc/gjban.html

Here's another discussion:

http://www.theserverside.com/news/1373391/Dependency-Injection-in-Java-EE-6-Part-1

"Managed beans are a key concept introduced in Java EE 6 to solve some of the limitations we talked about in the previous section with Java EE 5 style resource injection. A managed bean is just a bare Java object in a Java EE environment. Other than Java object semantics, it has a well-defined create/destroy life-cycle that you can get callbacks for via the @PostConstruct and @PreDestroy annotations. Managed beans can be explicitly denoted via the @ManagedBean annotation, but this is not always needed, especially with CDI. From a CDI perspective, this means that almost any Java object can be treated as managed beans and so can be full participants in dependency injection."

> If both your Entity and DAO are POJO's, why not
> simply instantiate the
> SiteDAO using a constructor or a factory? If you're
> looking for the CDI to
> do pooling or something like that, perhaps create a
> SLSB factory that can
> retrieve it via CDI and return it to your POJO?
> You'll need to use JNDI to
> etrieve a copy of your SLSB from the context.

My sample EAR was greatly simplified and probably a bit contrived. I'm not sure that I will keep the SiteDaoImpl as a POJO in my real app. I may make it a singleton EJB instead. But from everything I've read, CDI should work between POJOs, as I have it setup. The fact that it doesn't seems to imply another bug in GlassFish, which I'd be happy to submit a defect for if someone can confirm this.

Thanks,
-Dan
[Message sent by forum member 'theodan']

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