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 12:48:59 +0100

2009/8/17 Paul Sandoz <Paul.Sandoz_at_sun.com>:
> On Aug 17, 2009, at 1:09 PM, James Strachan wrote:
>
> Hmm... perhaps even better one could inject the contextual information for a
>
> provider e.g.:
>
>  @Provides
>
>  public Logger providerLogger(InjectionContext ic) {
>
>        return Logger.getLogger(ic.getTarget());
>
>  }
>
> Agreed! I like the idea of just passing the binding annotation or the
> injection context as a parameter to @Provides methods. (I really like
> @Provides methods!)
>
>
> Me too.
>
> BTW the spi package already has a nice InjectionPoint class ready to
> be passed in to provider methods. Am sure it would be fairly easy to
> allow it to be passed into provider methods...
>
>
> OK.
>
>
> Then i might be able to do something for Jersey/JAX-RS like the following:
>
>  bindAnnotation(QueryParam.class).
>
>    toProvider(QueryParamProvider.class).
>
>    inScope(RequestScoped.class);
>
>
>  public class QueryParamProvider implements Provider<?> {
>
>    @Inject
>
>    public QueryParamProvider(InjectionContext ic) {
>
>       // Analyze type and annotations, throw exception if it cannot be
>
> supported.
>
>       // Store state on the provider
>
>    }
>
>    ...
>
>  }
>
> Yeah. The argument crazybob often uses on the list is that you can
> enumerate all values of your binding annotations and then bind a
> specific value (or set of values) for an annotation to a specific
> instance/provider in the binding. But that feels kinda dirty to me!
>
> I would be willing to compromise on the above if InjectionPoint could be
> injected, small steps :-)
> I guess that would mean we do not require a bindAnnotation method and could
> do just:
>   bind(String.class).annotatedWith(QueryParam.class).toProvider(QueryParamProvider.class);
> (I am not sure if Guice will barf if other annotations are present with
> QueryParam, e.g. @DefaultValue).

Guice barfs if more than one annotation is used which is annotated
with @BindingAnnotation; so maybe you can avoid the @BindingAnnotation
on @DefaultValue?

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