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:
You are creating the
SubResource
instance yourself via thenew
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:
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:
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
: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.