users@jersey.java.net

Re: [Jersey] Using Guice with JAX-RS

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 29 Sep 2008 22:09:29 +0100

Hi Gili,

On Sep 29, 2008, at 9:36 PM, Gili wrote:

>
> According to http://architects.dzone.com/articles/putting-java-rest
> javax.ws.rs.core.Application.getSingletons() should be used if you
> want to
> use custom injection (such as Spring or Guice) but this makes little
> sense
> to me because it restricts you to singleton injection (that is, once
> you
> return an instance it lives forever).
>

I agree, but i am not sure Bill states it in such terms as you put it:

   You use this method when you want to have control over instance
creation of your resource classes and
   providers. For example, maybe you are using Spring to instantiate
your JAX-RS objects, or you want to register
   an EJB that uses JAX-RS annotations.

i.e. he presents an example of why it may be used in some circumstances.

I have used that mechanism to integrate automatic look up by JNDI name
e.g. if an interface is registered in the classes then an
implementation is looked up in JNDI using the fully qualified class
name as the interface name. This works rather nicely for EJBs with
GlassFish.


> Wouldn't it make more sense to have Set<MyProvider> getProviders()
> where
> MyProvider is a class factory similar to Guice "Provider"? This
> would let
> you use Guice to inject your instances and potentially use the HTTP-
> request
> scope.
>

We have avoided specifying all this because we need to coordinate with
EE 6 and use the support for injection and component models offered by
that, hopefully by leveraging WebBeans.

In Jersey we have support for Spring (and developers have experimented
with support for Guice and WebBeans) for per-request and singleton
life-cycles using implementations of:

   com.sun.jersey.spi.service.ComponentProvider

see the JavaDoc here [1]. This interface was built up over the
developer experience and feedback from Spring/Guice investigatons.

Paul.

[1] http://download.java.net/maven/2/com/sun/jersey/jersey-core/1.0-ea-SNAPSHOT/jersey-core-1.0-ea-SNAPSHOT-javadoc.jar