users@jersey.java.net

Re: [Jersey] How to create 'global handles' with JAX-RS?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 02 Oct 2009 13:21:45 +0200

On Oct 2, 2009, at 1:02 PM, Jan Algermissen wrote:

> Sorry if this question is somewhere in the docs, but I am unable to
> access the Web site.
>

> What is the intended way of managing 'global' objects such as
> database connections with JAX-RS? Since there is no init() method on
> the Resource classes or any form of injection, how do I initialize
> and access per-servlet global objects?
>

You can inject ServletContext and ServletConfig. e.g.:

   @Context ServletConfig sConfig;

When EE 6 is ready it will be possible to avail of the full injection
of EE resources as one can when say using Servlet. I am in the process
of implementing support for this.

Jersey also has a mechanism to plug-in custom injections. Some
developers have been using this in the interim of waiting for full EE
6 support, mainly for injecting EJB references. I can provide more
details as required.

Note that one can also use JNDI directly, and one can developer a
Jersey-based injectable provider that utilizes JNDI. For example, see
Gerard's blog entry:

   http://kingsfleet.blogspot.com/search?q=InjectableProvider


> Is this built-in or do I have to pull in something like Spring?
>

You can use Spring (or Guice with GuiceyFruit).

But, if you want a lighter-weight solution in the interim of Java EE 6
i recommend utilize one of the two JAX-RS and Jersey solutions
previously mentioned.

Paul.