Discussion:
Managed sub-resources
John Lister
2012-01-07 17:31:02 UTC
Permalink
I may have missed the answer to this, but is it possible to have managed
sub-resources such that I can inject beans, db resource, etc into them.

For example if I have
@Path("/child/")
Child getChild(){
return new Child();
}

then I have to either pass all the dependant resources as parameters or
make them visible to the child class so that they can be accessed from
it. The only way I can see is to make the child a bean and inject it
into the parent and return the injected object, the downside is the
extra object creation for every call.

Is there an alternative, how does everyone else do it?

Thanks
--
www.pricegoblin.co.uk
Marshall Pierce
2012-01-07 18:05:26 UTC
Permalink
Post by John Lister
I may have missed the answer to this, but is it possible to have managed
sub-resources such that I can inject beans, db resource, etc into them.
For example if I have
@Path("/child/")
Child getChild(){
return new Child();
}
then I have to either pass all the dependant resources as parameters or
make them visible to the child class so that they can be accessed from
it. The only way I can see is to make the child a bean and inject it
into the parent and return the injected object, the downside is the
extra object creation for every call.
Is there an alternative, how does everyone else do it?
Thanks
Looks like Jersey will not do this.
http://jersey.java.net/nonav/documentation/latest/user-guide.html
section 2.6 says:

----
Note that the runtime will not manage the life-cycle or perform any
field injection onto instances returned from sub-resource locator
methods. This is because the runtime does not know what the life-cycle
of the instance is.
----

If you're using an IoC container that supports JSR-330, you can use a
javax.inject.Provider (or a com.google.inject.Provider if you're using
Guice anyway) to accomplish this tidily. Add the Provider<Child> as a
constructor parameter, store it as a field, and call get() on it when
you need a Child object.
Cameron Heavon-Jones
2012-01-09 12:47:46 UTC
Permalink
I may have missed the answer to this, but is it possible to have managed sub-resources such that I can inject beans, db resource, etc into them.
For example if I have
@Path("/child/")
Child getChild(){
return new Child();
}
then I have to either pass all the dependant resources as parameters or make them visible to the child class so that they can be accessed from it. The only way I can see is to make the child a bean and inject it into the parent and return the injected object, the downside is the extra object creation for every call.
Is there an alternative, how does everyone else do it?
Thanks
--
www.pricegoblin.co.uk
You may find that ResourceContext.getResource(Class) is what you are looking for. Here is a previous discussion:

http://jersey.576304.n2.nabble.com/injecting-Context-fields-on-sub-resources-td4930958.html

Thanks,
Cam
John Lister
2012-01-10 07:58:45 UTC
Permalink
Post by Cameron Heavon-Jones
You may find that ResourceContext.getResource(Class) is what you are
http://jersey.576304.n2.nabble.com/injecting-Context-fields-on-sub-resources-td4930958.html
Thanks, Cam
Cheers, missed that and looks ideal.. On a side note, the discussion
also show resources extending a class like so:

@Path("/blah/")
class MyResource extends Base {
...
}

I was wondering if injection into Base now supported? For example this
would allow a common base class to include stuff like references to an
entity manager, etc rather than declaring it in every resource object.

John
--
www.pricegoblin.co.uk
Cameron Heavon-Jones
2012-01-10 11:13:01 UTC
Permalink
Post by John Lister
You may find that ResourceContext.getResource(Class) is what you are looking for. Here is a previous discussion: http://jersey.576304.n2.nabble.com/injecting-Context-fields-on-sub-resources-td4930958.html Thanks, Cam
@Path("/blah/")
class MyResource extends Base {
...
}
I was wondering if injection into Base now supported? For example this would allow a common base class to include stuff like references to an entity manager, etc rather than declaring it in every resource object.
John
--
www.pricegoblin.co.uk
Yes i think that is supported, you should be able to add @Context annotations to interfaces and inherited methods too.

cam

Loading...