I’ve got an app that is trying to use @EJB annotation to inject remote references to EJBs in my ejb.jar file. I’m getting inconsistent results. In one case, I have a listener in web.xml that gets called and apparently has the EJB injected correctly, since I can see it connecting to the EJB and calling methods on it. In another class (a struts2 action) I get NPE when it tries to access the EJB reference. As far as I can tell, they’re identical calls, in Java classes that live in the same .war file.
As a work around, I added code in the constructor to lookup the EJBs by their global JNDI names, and it works fine. I just can’t figure out why one @EJB works and not another.
How do you inject EJBs in Struts 2 actions? Are you using CDI? Are you using the Struts2 CDI plugin?
Update: The problem is that the container is not creating the Struts objects, Struts is, so the container doesn’t get the opportunity to inject anything. You’ll have to use the mentioned plugin for CDI to enable injection in your actions.
If you want to give it a try, get Struts 2 sources:
Then
cdinto thestruts2directory and run the following command (this will compile the required modules for thestruts-cdi-plugin)Then get the sources of the cdi-plugin:
And compile it:
Now, with the following dependencies in my pom.xml:
I was able to get an EJB injected in an Action:
See https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2-cdi-example/ for an example.