users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 17 Aug 2009 13:40:21 +0200

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).


>
>>> 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
>>>
>>
>> OK.
>>
>> In your experience how hard would it be to modify Guice to support
>> this for
>> @Inject?
>
> I don't think it'd be that hard to patch guice to allow the
> InjectionPoint to be passed into a @Provides method; though getting
> the guice folks to accept a patch is way harder :)
>

:-)

Paul.