I'm trying to circle something around myself: Injection only works at the first level of my application, but then it stops and all @Inject annotated properties are null in the objects returned from my factory template implementation. I read a lot about people who have problems with CDI to work with JAX-RS, but that doesn't seem to be a problem. It feels like I'm missing some annotations, or I donβt see a tree in front of all the trees (as we say here); -)
Edit: Was there a sample project with the code that I posted here for a double check. Now I understand that I am more simplified: I actually use the Factory Template to get my service, which can interrupt the managed context. See Advanced Example:
Let go.
First layer: JAX-RS application, all is well
@RequestScoped @Path("/somePath/someResource") public class SomeResource { @Inject ISomeServiceFactory someServiceFactory; @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public SomeResponse someMethod(@PathParam("foo") final String foo) { ISomeService myService = someServiceFactory.getService(ServiceCatalog.valueOf(foo)); // works, we jump in there! myService.someMethod(); } } public enum ServiceCatalog { STANDARD("standard"), ADVANCED("advanced"), FOO("foo"); // ... }
A broken factory service that selects an implementation based on known parameters (Enum) from the Call REST API:
public interface ISomeServiceFactory { public ISomeService getService(ServiceCatalog serviceType); } @Stateless public class SomeServiceFactory implements ISomeServiceFactory { public ISomeService getService(ServiceCatalog serviceType) { if(serviceType.equals(ServiceCatalog.FOO)) { return new FooService();
Second level: some EJB problems, here
// Interface: public interface ISomeService { public void someMethod(); } // Implementation: @Stateless public class SomeService implements ISomeService { @Inject private ISomeDao someDao; // this will be null !!! @Override public void someMethod() { someDao.doSomething() // exception thrown as someDao == null } }
Tao, which is supposed to be introduced
public interface ISomeDao { public void someMethod(); } @Stateless public class SomeDao implements ISomeDao { public void someMethod() {} }
Now at runtime, WebSphere Liberty tells me (among other bindings ...) the following:
com.ibm.ws.ejbcontainer.runtime.AbstractEJBRuntime I CNTR0167I: The server is binding the com.ISomeDao interface of the SomeDao enterprise bean in the my.war module of the my application. The binding location is: java:global/my/SomeDao!com.ISomeDao
I have beans.xml:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all"> </beans>
It seems that the new SomeService () breaks everything, could you please advise how to implement the factory so that CDI moves further along the road? Thanks.