users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 24 Mar 2009 14:44:38 +0100

On Mar 24, 2009, at 2:06 PM, Ian Clarke wrote:

> On Tue, Mar 24, 2009 at 1:56 AM, Craig McClanahan <Craig.McClanahan_at_sun.com
> > wrote:
> If what you want is stored as a servlet context attribute (which you
> did not state AFAICT in your initial problem description)
>
> I didn't state this because I didn't want to preempt the "right" way
> to do this.
>
> , you can inject a ServletContext object already and have total
> access to it's capabilities. No extra code required ... and it's
> independent of which JAX-RS framework you choose also.
>
> Can you point me to some simple example code that demonstrates this?
>

You can do this:

   @Path("/")
   public class MyResource {

     @Context ServerContext sc;

   }

See here for some more details on injection:

   http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features#OverviewofJAX-RS1.0Features-RulesofInjection

Paul.