xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Child injector injecting root injector rather then itself #472

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi everyone,

according to the JavaDoc of com.google.inject.Injector all injectors are 
supposed to have a binding 
from Injector.class to themselves. Nevertheless, the child Injector seems to 
inject its root injector 
rather then itself. I attached a short example code.

I guess that either the documentation or the implementation is faulty.

Cheers

Original issue reported on code.google.com by an.pere...@googlemail.com on 15 Apr 2010 at 10:54

Attachments:

GoogleCodeExporter commented 9 years ago
Alright, I figured out a workaround: one must bind the class into which the 
child injector is going to be 
injected explicitly in he child injector's bindings. For my previous example, 
this would mean something like 
this: bind(A.class);

Nevertheless, I consider the current behavior a bug. 

Original comment by an.pere...@googlemail.com on 15 Apr 2010 at 11:04

GoogleCodeExporter commented 9 years ago
This is because if the binding does not exist in the child injector's module, 
Guice
will create it as a "just in time" binding, and JIT bindings are created in the
parent modules (for a variety of reasons).

Original comment by sberlin on 15 Apr 2010 at 12:53

GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 25 Apr 2010 at 10:17