users@glassfish.java.net

Re: CDI Instances and EJB and Glassfish

From: <glassfish_at_javadesktop.org>
Date: Mon, 19 Jul 2010 14:51:52 PDT

Slowly isolating this down.

Turns out this is visible even without explicit reliance on CDI.

Root question: why, when I inject a stateless session bean into a servlet, can that SLSB's injected @Resource not be found?

(For anyone following along, I am using Vaadin, a most excellent UI toolkit. Vaadin applications stem from a root Application class that is stored in the session. If you want to use EJBs in that application, as I do, you normally use CDI, as explained here, in option 2: http://vaadin.com/wiki/-/wiki/Main/Creating%20JEE6%20Vaadin%20Applications While attempting to get this to work, I've been encountering problems with CDI, or so I thought. I've backed all the way up to working directly with the Vaadin servlet to eliminate as much CDI as possible.)

I have a servlet (that extends the AbstractApplicationServlet class documented here: http://vaadin.com/api/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.html). To see if the problem was CDI-related or not, I temporarily injected my session bean into the servlet directly, using the @EJB annotation--thus completely eliminating CDI from the picture. I then tried to call a method on that object, just to make sure the proxy was initialized properly. Everything blows up.

The session bean in question has a @Resource field annotated like this:
[code]
@Resource
private KnowledgeBase kb;
[/code]

...and the resource adapter that vends KnowledgeBase instances is present in the ear file. I've also verified in a separate unit test using the Glassfish embedded EJB container that this resource injection works--at least in the unit test.

The EJB implementation is injected into the servlet's private instance variable--i.e. it's non-null--but when I call a method on it I get the stack trace in the attached file (the stack is monstrously huge). You'll note that Weld somehow gets involved even though I'm not using @Inject anywhere. It is true that the EJB jar is a bean archive.
[Message sent by forum member 'ljnelson']

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