简体   繁体   English

Subresouce和CDI注入问题

[英]Subresouce and CDI Injection Issue

I am new to jax-rs and i am stuck with subresources. 我是jax-rs的新手,我遇到了子资源问题。 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? 为什么CDI Injection可以与ResoureContext一起使用?

This has nothing do to with subresources or JAX-RS. 这与子资源或JAX-RS没有任何关系。 In principle, this is about how CDI injection works. 原则上,这是关于CDI注入的工作原理。

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. 您正在通过new关键字自己创建SubResource实例。 Therefore CDI has no clue about it existing and has absolutely zero control over such object. 因此,CDI对其存在没有任何线索,并且对这种对象完全没有控制权。 Therefore, CDI cannot inject anything into this object. 因此,CDI无法向此对象注入任何内容。

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. 在这种情况下,您注入了一个上下文(已经是CDI托管的“对象”)并告诉它为您检索资源。 Therefore you let the CDI container handle the object creation and its lifecycle. 因此,您让CDI容器处理对象创建及其生命周期。 And since it manages creation, it can also resolve injection points and inject MyBean . 由于它管理创建,它还可以解析注入点并注入MyBean

Generally, when you want to use CDI, you barely ever create objects via new . 通常,当您想要使用CDI时,您几乎不会通过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 : 在第二种情况下,您已允许JAX-RS容器创建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. 这使它能够控制对象生命周期,使其有机会执行注入和其他生命周期操作,例如执行@PostConstruct注释方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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