users@jax-rs-spec.java.net

[jax-rs-spec users] [jsr339-experts] Re: Re: Back To DI in Subresources

From: Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>
Date: Tue, 17 Apr 2012 11:37:31 -0400

On Apr 17, 2012, at 7:45 AM, Marek Potociar wrote:

> 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?

 Why not both? As you say, (1) is more flexible but (2) is quite convenient.

-- Santiago

>> 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
>