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 :-)
Thanks though - its good to know there is a way to do it!
> 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).
Though if I'm creating the sub resource object by hand, there will be
no post construction method invoked anyway - we're already outside of
the IoC post construction rules, since the IoC container isn't
creating the object - we're just invoking a constructor on the object.
It would be nice to be able to enable JAXRS injection into sub
resources - maybe as an optional extension?; it would leave the code
more dry. I find when creating sub resources I tend to create a custom
constructor for the root resource to invoke to keep code small & DRY.
It does seem a bit odd that you can share a base class/trait between a
root and sub resource and its not possible to have any of the
injections fire in the sub resources.
I wonder how hard it'd be to add as an option? I remember seeing in
both Guice and Spring for example a one line method call to perform
any remaining injections on an object if you create it outside of the
IoC container?
--
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/