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 17:10:40 +0200

On Apr 30, 2010, at 4:56 PM, James Strachan wrote:

> On 30 April 2010 15:48, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>>
>> On Apr 30, 2010, at 4:07 PM, James Strachan wrote:
>>
>> I started tinkering with some Guice IoC in a Scalate Jog project...
>> http://scalate.fusesource.org/documentation/jog.html
>>
>> and was quite suprised to not have a resource injected when using
>> Guice's @Inject. Turns out I think that the code only attempts to use
>> Guice to inject if it can see an @Inject annotation on a
>> constructor -
>> or the resource is bound with the module.
>>
>>
>> Yes.
>>
>> I just wondered why this was done?
>>
>> It simply did not occur to me to check for other uses of @Inject. I
>> was just
>> looking for an explicit marker to ensure Guice support would not
>> gobble up
>> all classes to be instantiated by Guice (see later).
>>
>> I figured at least check for
>> method/field annotations too? I've raised an issue to track it...
>> https://jersey.dev.java.net/issues/show_bug.cgi?id=520
>>
>> Maybe we should just change the
>> GuiceComponentProviderFactory.isImplicitGuiceComponent method to
>> check for @Inject on any field or method too?
>>
>>
>> Yes, i think we can modify to do more checking. Basically if it
>> quacks like
>> a Guice component but is not bound let Guice instantiate it.
>>
>> Would it be too bad to just assume Guice is gonna create an
>> instance I
>> wonder?
>>
>> The problem is that thorny issue of constructor injection:
>> public class MyClass {
>> public MyClass(@PathParam("p") String p) {
>> ...
>> }
>> }
>> thus we don't want Guice to instantiate stuff like the above.
>> Perhaps we can try these rules:
>> 1) check if there is a constructor annotated with @Inject, if not
>> 2) check for fields/methods annotated with @Inject.
>> and let Guice throw up errors if construction fails?
>> Do you know if it is possible to pass an instance to Guice to be
>> injected
>> on? If so we might be able to support Jersey constructing +
>> injecting, then
>> Guice injecting.
>
> Yes.
>
> injector.injectMembers(object)
>
> http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/Injector.html#injectMembers(java.lang.Object)
>
> I've just hacked up a test case to try use this method - but have been
> getting some strange failures. (You might be able to figure this out
> quicker than I can).
>
> So I think the current behaviour of looking for a constructor with
> @Inject is correct; its just we also need to use
> injector.injectMembers(object) on objects which Jersey creates.
> Haven't tested yet if that call fails if there's no @Inject
> annotation; I suspect not.

I will try some tests. I think i might be able to reuse:

   IoCProxiedComponentProvider

and instead of proxying the implementation will perform injection if
@Inject annotated fields and methods are present.

Paul.