users@jersey.java.net

Re: [Jersey] Newbie question: How do I inject objects into resources

From: Erdinc Yilmazel <erdinc_at_yilmazel.com>
Date: Tue, 24 Mar 2009 00:19:27 +0000

Hi,

You may write your own Injectables and InjectableProviders for
injecting custom objects. You may even define your own annotations for
that. See :

com.sun.jersey.spi.inject.Injectable
com.sun.jersey.spi.inject.InjectableProvider
com.sun.jersey.spi.inject.Inject

After writing and InjectableProvider you must annotate it with
@Provider annotation to make Jersey aware of that class.

The @Context annotation has also its InjectableProvider.

The other choice is using a IOC framework like google guice. Support
for google guice may be bundled with the upcoming 1.0.3 version of
Jersey, but you can find an example code in jersey subversion
repository.

Erdinc

On Mon, Mar 23, 2009 at 11:33 PM, Ian Clarke <ian.clarke_at_gmail.com> wrote:
> I have an object, lets say a SQL connection, that I want my Jersey resource
> classes to have access to, but I don't want to make the SQL connection
> static (because some day there may be more than one of them).
>
> How do I inject an object like this into a resource class so that it can use
> it?  For that matter, how do I tell resource classes about any objects
> without making them available statically?
>
> I have a feeling it may be something to do with the @Context annotation, but
> I haven't been able to find clear documentation on this.
>
> Any help would be appreciated,
>
> Ian.
>