users@jersey.java.net

Re: [Jersey] Guice in 0.8

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 15 May 2008 18:45:24 +0200

Hi Zach,

Zach Cox wrote:
> I found Christian Rivasseau's great work on Guice + Jersey:
>
> http://objectif-naiade.blogspot.com/2007/11/integrating-jersey-with-guice-and.html
>
> But that code was written back in Nov 2007 and the ResourceProvider
> interface is much different now in 0.8. Is there any code around to
> easily use Guice to inject field/method parameters in resource classes
> in 0.8? Or are there any tutorials/code examples I could study to
> create Guice integration in 0.8?
>

If you are familiar with Guice, perhaps looking at a WebBeans example
might help [1].

Plus you can look at the Spring code here [2].

Things have changed to use the ComponentProvider interface. You can
probably hack things together to get things working by just implementing
the ComponentProvider.getInstance() method:


     public static class GuiceComponentProvider
             implements ComponentProvider {

         public <T> T getInjectableInstance( T instance ) {
             return instance;
         }

         public SpringComponentProvider(
                 ConfigurableApplicationContext springContext) {
             this.springContext = springContext;
         }

         public <T> T getInstance( Scope scope, Class<T> clazz )
                 throws InstantiationException, IllegalAccessException {

             // Insert Guice specific code here to get the instance
         }

         public <T> T getInstance(Scope scope,
                 Constructor<T> constructor,
                 Object[] parameters)
                 throws InstantiationException, IllegalArgumentException,
                 IllegalAccessException, InvocationTargetException {

             return getInstance(scope, constructor.getDeclaringClass());
         }

         public void inject(Object instance) {
         }
     }

Paul.

[1]
http://stephan.reposita.org/archives/2008/02/25/adding-web-beans-jsr-299-to-jersey-for-rest/
[2]
https://jersey.dev.java.net/source/browse/*checkout*/jersey/trunk/contribs/spring/src/main/java/com/sun/jersey/spi/spring/container/servlet/SpringServlet.java?rev=998


-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109