users@jersey.java.net

[Jersey] Re: Managed sub-resources

From: Cameron Heavon-Jones <cmhjones_at_gmail.com>
Date: Mon, 9 Jan 2012 12:47:46 +0000

On 07/01/2012, at 5:31 PM, John Lister wrote:

> 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