users@jersey.java.net

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

From: James Strachan <james.strachan_at_gmail.com>
Date: Mon, 17 Aug 2009 09:15:27 +0100

2009/8/14 Paul Sandoz <Paul.Sandoz_at_sun.com>:
> Hi Craig,
> What is the scope of MyResource? have you bound it to a Scopes.SINGLETON
> ? If so i am guessing the MyResource class is instantiated by Guice outside
> the scope of the request/response.
> You could do the following to bind WebApplication:
>         protected void configure() {
>             ... bindings for my application objects ...
>             bind(WebApplication.class).
>                 toInstance(WebApplicationFactory.createWebApplication()).
>                 in(Scopes.SINGLETON);
>         }
> Of course it needs to be initiated, which i presume you are doing.
> Feel free to send me a copy of your project and i can investigate as well.
>
> For correctly support for injection of UriInfo (etc.) for singletons we need
> to have the thread local proxy instance of that class so it can be reliably
> injected onto a singleton. An alternative is to inject Provider<UriInfo>
> then the existing approach should work.
> Like Richard i too have not had any time to follow up on this.
> In Guice 2.0 it is not possible to support the following:
>     @Path("foo")
>     @Encoded
>     public class MyResource {
>
>         @Inject
>         public class MyResource(@DefaultValue("x") @QueryParam("x") String
> x) {
>             this.x = x;
>         }
>
>         private final String x;
>
>         ...
>
>     }
>
> Guice's approach to custom injections using a TypeListener has
> limitations because it means one cannot integrate cleanly with the parameters associated with @Inject on constructors, fields or methods.

Agreed

> I would like to do something like:
>   MetaProvider mp = ...
>   bind(QueryParam.class).toMetaProvider(mp);
> Where a meta provider has access to the injection type, the annotations on
> the type, and the target class. The meta provider is responsible for
> creating a provider with the appropriate scope.
> I am very very tempted to modify Guice to support this concept.

I'd love this too! Basically being able to use the binding annotation
as a parameter in a provider method along with the injection point
(method/field); so you can have a single provider (class/method) which
takes the binding annotation as a parameter - then you can use its
attributes to create the value.

This came up recently on the guice list though unfortunately didn't
seem to get much traction from guice committers
http://groups.google.com/group/google-guice/msg/aafaad43d9433bbd

FWIW GuiceyFruit provides something like this; you create a
implementation of this interface...
http://code.google.com/p/guiceyfruit/source/browse/trunk/guiceyfruit-core/src/main/java/org/guiceyfruit/support/AnnotationMemberProvider.java

which then has access to the annotation and the method/field for
creating the value. The only downside is this takes place at
field/method injection points (not constructors) as its a separate
injection mechanism to @Inject such as to support @Resource,
@Autowired and so forth.

-- 
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/