Subresouce and CDI Injection Issue

2019-07-31 07:01发布

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?

2条回答
女痞
2楼-- · 2019-07-31 07:36

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.

查看更多
我命由我不由天
3楼-- · 2019-07-31 07:53

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.

查看更多
登录 后发表回答