users@jersey.java.net

Re: [Jersey] injecting _at_Context fields on sub resources...

From: James Strachan <james.strachan_at_gmail.com>
Date: Wed, 30 Jun 2010 15:23:49 +0100

On 20 April 2010 15:01, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
> Hi James,
>
> There are currently two ways for the application to instantiate resource
> classes:
>
> 1) Injected @Context ReosurceContext rc then call
> rc.getResource(MyClass.class);
>
>
>  https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/ResourceContext.html
>
>   def getElement(@PathParam("id") key : String, @Context ReosurceContext rc)
> {
>     doSomething;
>     val e = rc.get(classOf[Element]);
>     e.setKey(e);
>     return e;
>   }
>
> 2) Using Jersey's @Inject (i know it is an unfortunate clash with 330).
>
>   def getElement(@PathParam("id") key : String, @Inject Injectable<Element>
> ie) {
>     doSomething;
>     val e = ie.get();
>     e.setKey(e);
>     return e;
>   }
>
> None are quite what you want :-)
>
> Injecting after instantiation sort of breaks the life-cycle contract
> (injection onto fields or setter methods should occur after construction and
> before any post construct method is invoked).

A colleague hit this again today so I was wondering if we could come
up with a clean canonical way to get Jersey create sub resources for
us doing all the lifecycle/injection goodness without using the
ResourceContext/Injectable API?

e.g. something like...

class Container extends Foo {
 @GET
 @Path("{id}")
 def getElement(subResource: Element) = {
   // possibly modify the object we are given, or return something else...
   subResource
 }
 ...
}

class Element(@PathParam("id") key: String) extends Foo {
 @GET
 def index = {
  ...
 }
...
}

We might want to annotation the getElement's subResource parameter
somehow to indicate to Jersey its meant to inject a sub resource into
the method call?

-- 
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/