-
15. Re: Problems with stateless beans in Weld CR 1.0.1 CR2
atijms.arjan.tijms.m4n.nl Apr 16, 2011 6:06 PM (in response to andygibson.contact.andygibson.net)I encountered the same problem in JBoss AS 6 Final. I'm trying to inject a local stateless session bean using @Inject, but I too get the dreaded
WELD-000079 Could not find the EJB in JNDI
There's also the following root exception:
java.lang.NullPointerException org.jboss.ejb3.endpoint.deployers.DefaultEJBIdentifier.identifyEJB(DefaultEJBIdentifier.java:63) org.jboss.ejb3.endpoint.deployers.DefaultEndpointResolver.resolve(DefaultEndpointResolver.java:37) org.jboss.weld.integration.ejb.JBossSessionObjectReference.<init>(JBossSessionObjectReference.java:61) org.jboss.weld.integration.ejb.JBossEjbServices.resolveEjb(JBossEjbServices.java:65) org.jboss.weld.bean.SessionBean.createReference(SessionBean.java:476)
My deployment is an EAR and I don't have the option to change this into a WAR only deployment.
Does @Inject really only works in WAR deployments?
-
16. Re: Problems with stateless beans in Weld CR 1.0.1 CR2
atijms.arjan.tijms.m4n.nl Apr 16, 2011 6:42 PM (in response to andygibson.contact.andygibson.net)I found a workaround. It's a little tedious, but if I create an EJB in the web module it's possible to inject this with @Inject. This EJB on its turn can be injected with the EJB from the EJB module in the EAR using the @EJB annotation:
// This bean is defined in the WEB module @Stateless public class UserDAOSilly implements UserDAOSillyLocal { @EJB private UserDAO userDAO; // definition lives in EJB module @Override public UserDAO getUserDAO() { return userDAO; } }
// This bean is also defined in the web module @RequestScoped public class MyBean { @Inject private UserDAOSilly userDAOSilly; // injection works public void test() { UserDAO userDAO = userDAOSilly.getUserDAO(); // works, but a little silly } }
-
17. Re: Problems with stateless beans in Weld CR 1.0.1 CR2
atijms.arjan.tijms.m4n.nl Apr 17, 2011 6:41 AM (in response to andygibson.contact.andygibson.net)As it appears, producer methods also work, so a slightly less silly workaround is the following:
// This bean is defined in the WEB module @Stateless public class EJBFactory { @EJB protected UserDAO userDAO; // ~X other EJBs injected here @Produces @EJBBean public UserDAO getUserDAO() { return userDAO; } // ~X other producer methods here }
Now EJBs from anywhere in the EAR can be injected with:
// This bean is also defined in the web module @RequestScoped public class MyBean { @Inject @EJBBean private UserDAO userDAO; // injection works public void test() { userDao.getByID(...); // works } }
EJBBean is a simple standard qualifier annotation. For completeness, here it is:
@Qualifier @Retention(RUNTIME) @Target({TYPE, METHOD, FIELD, PARAMETER}) public @interface EJBBean { }
Should Weld ever decide to support injecting EJBs from anywhere in the EAR module directly (or fix the bug, if it appears to be a bug), one can just remove the @EJBBean qualifier.
The downside of course is that you have to maintain the Factory bean in the meantime. If you have a complicated EAR with multiple web modules that need EJB injection, the factory has to be copy/pasted. In the case of many EJBs (say 80) and many web modules, this could become quite hairy.