users@jersey.java.net

Re: [Jersey] Guice injection does not support field/method injection without forcing you to bind the Resource class to the module...

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 30 Apr 2010 18:18:38 +0200

On Apr 30, 2010, at 5:41 PM, James Strachan wrote:

> On 30 April 2010 16:36, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>> Hi James,
>>
>> You beat me to it :-)
>>
>> I think we may be able to refine things as i got the following to
>> work:
>>
>> @Path("unbound/perrequest")
>> public static class InjectedPerRequestResource {
>>
>> @Context UriInfo ui;
>>
>> String x;
>>
>> @Inject
>> GuiceManagedClass gmc;
>>
>> public InjectedPerRequestResource(@QueryParam("x") String x) {
>> this.x = x;
>> }
>>
>> @GET
>> @Produces("text/plain")
>> public String getIt() {
>> assertEquals("unbound/perrequest", ui.getPath());
>> assertEquals("x", x);
>>
>> return gmc.toString();
>> }
>> }
>>
>> Thus we can also support the case when Jersey instantiates and
>> Guice injects
>> too i.e. when there are constructors with no @Inject with @Injected
>> fields/methods.
>
> Great!
>

Refined.

Note that when you use GuiceManagedComponentProvider there is no need
to call the injectMembers because Guice is already instantiating. So i
have a question about that. The updated code does the following:

             } else if (isGuiceFieldOrMethodInjected(clazz)) {
                 // If there is a constructor with arguments then let
Jersey
                 // instantiate and manage the scope, let Guice inject
                 if (hasConstructorWithArguments(clazz)) {
                     LOGGER.info("Binding " + clazz.getName() + " to
GuiceInjectedComponentProvider");
                     return new
GuiceInjectedComponentProvider(injector);
                 } else {
                     // TODO should Jersey manage or Guice manage,
perhaps it
                     // depends if the component is not in the Guice
NO_SCOPE ?
                     ComponentScope componentScope =
getComponentScope(key, injector);
                     LOGGER.info("Binding " + clazz.getName() +
                             " to GuiceManagedComponentProvider with
the scope \"" +
                             componentScope + "\"");
                     return new
GuiceManagedComponentProvider(injector, componentScope, clazz);
                 }
             } else {

The current logic will check if there is a constructor with args, if
so we let Jersey instantiate and Guice inject. Otherwise there is no
constructor or a no-arg constructor. In this case we are currently
letting guice instantiate. But i wonder if for the case of Guice
NO_SCOPE Jersey should instantiate? i.e. it may be likely in this case
that Jersey-based scope annotations are used. I think given the other
cases Jersey is managing it may make sense to be consistent.

Paul.


>
>> I will merge things.
>
> Great stuff, thanks! :)
>
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>