users@jersey.java.net

Re: [Jersey] Guice access to Jersey-created objects

From: Richard Wallace <rwallace1979_at_gmail.com>
Date: Thu, 13 Aug 2009 14:00:34 -0700

On Thu, Aug 13, 2009 at 11:32 AM, Craig
McClanahan<Craig.McClanahan_at_sun.com> wrote:
> Furthering my explorations with integrating Jersey and Guice, I'd like to be
> able to inject Jersey-provided components into a Guice-instantiated service
> class.  As a particular example, I have a "transformer" class that turns my
> internal data model object into a suitable external representation,
> including some computed URIs.  For that, I need to have Guice able to inject
> things like the UriInfo object Jersey injects into resource classes.
>
> OK, I can bind a Provider<UriInfo> into Guice's injector ... but where does
> the implementation of this acquire the UriInfo instance from Jersey?  Any
> ideas on a strategy to make these kinds of things visible to Guice's
> injector?

What I've been doing is allowing Guice to create my WebApplication
instance and injecting that into my own GuiceServletContainer which
overrides the create() method to return the injected WebApplication.
If you do that, then you can do something like what I have here
<http://pastie.org/583188>. It's in Scala, but you should be able to
get the idea.

I also talked with Paul once about taking it a bit further. It
requires exposing more of the Jersey services so they could be
injected using Guice 2.0's custom integrations. The biggest drawback
is that they wouldn't be injectable into the constructor that way,
only on fields and methods. But it would be better than nothing I
figure. Unfortunately, I haven't found the time to follow up on this.
 Maybe you can?

Rich

[1] http://code.google.com/docreader/#p=google-guice&s=google-guice&t=CustomInjections
[2] http://n2.nabble.com/Enhanced-Guice-Jersey-integration-td3140841.html#a3140841