CDI does not work in objects created from factory template implementation

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(); // will break CDI context } else if (...) { // ... } else { return new DefaultService(); // will also break CDI context } } } 

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.

+5
source share
1 answer

In the modified question, you show that the EJB is not actually entered into the web service, it is updated indirectly through the factory.

An object has only a CDI injection performed in it when the container creates the object, either injected by itself somewhere, or is one of the managed components of the EE.

Net, you cannot create new EJBs or any CDI beans and have any CDI services in it.

+2
source

Source: https://habr.com/ru/post/1244683/


All Articles