@Inject Injector does not work with child injectors
See original GitHub issueThe testcase below works, if the injector is created by:
Guice.createInjector(new ChildModule());
it fails if the injector is created by:
Guice.createInjector().createChildInjector(new ChildModule());
The error is:
com.google.inject.ConfigurationException: Guice configuration errors:
- Unable to create binding for guice.GuiceTest$IOne. It was already configured on one or more child injectors or private modules bound at guice.GuiceTest$ChildModule.configure(GuiceTest.java:39) If it was in a PrivateModule, did you forget to expose the binding? while locating guice.GuiceTest$IOne
package guice;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
public class GuiceTest {
public interface IOne {
}
public static class One implements IOne {
}
public interface ITwo {
IOne getOne();
}
public static class Two implements ITwo {
@Inject Provider<Injector> provider;
@Override
public IOne getOne() {
return provider.get().getInstance(IOne.class);
}
}
public static class ChildModule extends AbstractModule {
@Override
protected void configure() {
bind(IOne.class).to(One.class);
bind(ITwo.class).to(Two.class);
}
}
@Test
public void thisTestWorks() {
Injector injector = Guice.createInjector(new ChildModule());
ITwo two = injector.getInstance(ITwo.class);
assertNotNull(two.getOne());
}
@Test
public void thisTestFails() {
Injector injector = Guice.createInjector().createChildInjector(new ChildModule());
ITwo two = injector.getInstance(ITwo.class);
assertNotNull(two.getOne());
}
}
Issue Analytics
- State:
- Created 8 years ago
- Reactions:6
- Comments:18 (6 by maintainers)
Top Results From Across the Web
@Inject Injector is injecting root injector rather than child injector
The easy answer is to simply bind the child injector using a provider. Dhanji.
Read more >Do child injectors in Guice ever get created automatically?
I've found references to child injectors inside the Guice documentation, but other than them being used explicitly I can't find any ...
Read more >Injector (Google Guice 5.0.1 API)
Builds the graphs of objects that make up your application. The injector tracks the dependencies for each type and uses bindings to inject...
Read more >Hierarchical injectors - Angular
With hierarchical dependency injection, you can isolate sections of the application and give them their own private dependencies not shared with the rest...
Read more >Child Injectors - WireBox : Dependency Injection & AOP For ...
Child Injector Methods ... Here are some of the new methods to assist with child injectors: hasChildInjector( name ) - Verify if a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sameb, that does, indeed, seem to be the bug - instances obtained through child injectors are handed the parent injector in some (but not all) cases:
Any chance this bug will get addressed?
So, the weird workaround that someone at my company seems to have figured out is that if you inject an injector along with something exclusively from the parent injector scope, the right injector gets injected.