Search code examples
javarestjakarta-eejax-rscdi

Subresouce and CDI Injection Issue


I am new to jax-rs and i am stuck with subresources. Take a look.

this is not working

@Path(..)
public class Test
{
 @Path(...)
 public SubResource getSub(){
  return new SubResource();
 }
}

public class SubResource {
 @Inject
 private MyBean myBean;

 @GET
 public String getStr(){
  return myBean.getStr(); // myBean is null, injection didnt work properly
}

this works, but why????

@Path(..)
public class Test
{
 @Context
 private ResourceContext context;

 @Path(...)
 public SubResource getSub(){
  return context.getResource(SubResource.class);
 }
}

public class SubResource{
 @Inject
 private MyBean myBean;

 @GET
 public String getStr(){
  return myBean.getStr(); // myBean is not null anymore, why?
}

Why CDI Injection works with ResoureContext?


Solution

  • This has nothing do to with subresources or JAX-RS. In principle, this is about how CDI injection works.

    Firstly, your not working sample. Or to be precise, this bit:

    @Path(...)
     public SubResource getSub(){
      return new SubResource();
     }
    

    You are creating the SubResource instance yourself via the new keyword. Therefore CDI has no clue about it existing and has absolutely zero control over such object. Therefore, CDI cannot inject anything into this object.

    Now to the working sample:

    @Context
     private ResourceContext context;
    
     @Path(...)
     public SubResource getSub(){
      return context.getResource(SubResource.class);
     }
    

    In this case, you injected a context (a CDI managed "object" already) and tell it to retrieve the resource for you. Therefore you let the CDI container handle the object creation and its lifecycle. And since it manages creation, it can also resolve injection points and inject MyBean.

    Generally, when you want to use CDI, you barely ever create objects via new. The obvious exception are producers, but we are not talking those here.