users@jersey.java.net

Re: [Jersey] Instatiating subResources with _at_Context class fields

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Mon, 2 Aug 2010 11:51:33 +0200

Hi,

On Aug 1, 2010, at 5:27 AM, tofferliu wrote:

>
> Hi,
>
> Is there a way to instatiate a subResource such that class member
> annotated
> with @Context will still be injected with their respective values?
>

Yes, you can inject ResourceContext [1]. Or inject the instance
directly using Jerseys @Inject annotation. If you do not want the
instance to be instantiated until you require it wrap the injection
target type in Injectable (or use ResourceContext) e.g.

   @Inject Injectable<SubResource> sr;


> The problem with the approach below is that it creates the subResource
> instance without injecting the value for "bar".
>
> -Toffer
>
> public class SubResource {
>
> @Context
> Foo bar;
>
> }
>
> @Path("/")
> public class MainResource {
>
> @GET
> public SubResource getSubResource() {
> return new SubResource();
> }

The above method is a resource method not a sub-resource locator,
because it is annotated with HTTP-based method annotation and not @Path.

> }


Paul.

[1] https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/ResourceContext.html

> --
> View this message in context: http://jersey.576304.n2.nabble.com/Instatiating-subResources-with-Context-class-fields-tp5360153p5360153.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>