There are 2 solutions that I've been thinking about:
introduce an injectable request-scoped ResourceContext with methods like inject(Class<?> subresourceClass), inject(Object subresource)
update the spec to mandate field injection on the sub-resource instances returned by sub-resource locator.
So far I am leaning towards #1 as it seems more flexible.
Any thoughts on the above?
Marek
On Apr 15, 2012, at 10:33 PM, Adam Bien wrote:
> Hi,
>
> I would like to re-activate the already mentioned issue of lack of support of DI in sub-resources.
>
> Usually you are going to separate JAX-RS resources and services into separate classes. In that case services are going to be injected into resource with @Inject (it works already fine in Java EE 6).
>
> However: usually JAX-RS runtime is navigating from the main Resource to the sub-resource class via the @Path annotation.
>
> As developer you are creating a sub-resource using an ordinary "new" and a constructor with parameters. The sub-resources instance is no more managed and injection is not available.
>
> Proposal: we need a way to pass an instance to a JAX-RS runtime to inject dependencies.
>
> Btw. parameterless constructors are not enough - usually you are going to pass some context from the main resource to the sub resources.
>
> See also: https://java.net/jira/browse/JAX_RS_SPEC-72
>
> thanks,
>
> adam