Subresouce and CDI Injection Issue

孤人 提交于 2019-12-24 11:19:04

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/42504072/subresouce-and-cdi-injection-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!