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).
Paul.
On Apr 20, 2010, at 2:51 PM, James Strachan wrote:
> if you had a Container and Element resources like this... (please
> excuse the scala syntax...)
>
> class Container extends Foo {
> @GET
> @Path("{id}")
> def getElement(@PathParam("id") key : String) = {
> doSomething
> new Element(key)
> }
> ...
> }
>
> class Element(key: String) extends Foo {
> @GET
> def index = {
> // this will fail here...
> doSomething
> }
>
> ...
> }
>
> abstract class Foo {
> @Context
> protected var servletContext: ServletContext = _
>
> def doSomething: Unit = {
> if (servletContext == null) throw IllegalArgumentException("no
> servletContext injected! Probably a sub resource :)")
> }
> }
>
>
> Using @Context injection is very handy; however right now only root
> resources get injected causing code like above to barf.
>
> Is there any way to get Jersey to do injection of user created sub
> resources (e.g. the Element instance) after its been created, but
> before its methods have been invoked.
> e.g. after the Container.getElement is invoked and an Element
> created, but before the Element.index method is invoked?
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>