简体   繁体   中英

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?

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.

Whenever you create an object like this:

return new SubResource();

then it's lifecycle belongs to you and no injection is performed on it.

In the second case you have allowed the JAX-RS container to create the SubResource :

return context.getResource(SubResource.class);

which gives it control of the object lifecycle, giving it the opportunity to perform injection and other lifecycle operations such as executing @PostConstruct annotated methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM