Problem
Injecting an enterprise java bean with @EJB
in an application client, launched with Java web start and from a GlassFish 3.1.2.2 (build 5) server.. works as long as the requirements are fulfilled [see note at bottom]. But all fails if I inject something using @Inject
, despite whatever resource type we're trying to inject. And I know it failed because I shall get a NullPointerException
if I try to use the injected resource. That tells me the annotation processor has completely forgot @Inject
.
Not only am I intrigued that @EJB
work flawlessly at the same time @Inject
does not work at all, I also reason that this "silent failure" of the injection must be a violation of the Java EE 6 specification. The specification says on page 69:
If the container fails to find a resource needed for injection, initialization of the class must fail, and the class must not be put into service.
This issue could be related to, or an expression of, a bugg in GlassFish.
My end goal..
.. is to separate API from implementation so that I get a much easier job trying out different implementations when it's time for performance testing and profiling my application. However I take it this design pattern is pretty much impossible to recreate in an application client then? My immediate approach is to create new instances of all the implementations in the Main
class or use factories. Of course the best solution of all would be to use qualifiers!
Note: Requirements for injection in application clients are that CDI must be enabled for the component (package must contain the file beans.xml, most likely empty if you're used to annotations!) and support of CDI in application clients is actually optional, implementation specific. Moreover, if CDI is enabled and supported, then you should know that the application client container can only be active during the boot of the client and requires thus that the resources be injected into the Main
class and that the fields are static
.