I have following Guice private module dependency tree:
public class FooModule extends PrivateModule {
@Override
protected void configure() {
install(new BarModule());
bind(Person.class);
expose(Person.class);
}
}
public class BarModule extends PrivateModule {
@Override
protected void configure() {
install(new LifeImplModule());
bind(Animal.class);
expose(Animal.class);
}
}
public class LifeImplModule extends PrivateModule {
@Override
protected void configure() {
bind(Life.class).toInstance(new LifeImpl());
expose(Life.class);
}
}
public class Animal {
@Inject
public Animal(Life life) {
//
}
}
public class Person {
@Inject
public Person(Life life) {
//
}
}
Now when I do Guice.createInjector(new FooModule()).getInstance(Person.class)
- it fails since it doesn't recognize binding for Life.class
whereas Guice.createInjector(new FooModule()).getInstance(Animal.class)
works since it has binding for Life
via BarModule
.
How do I go about solving this issue? I tried moving
install(new LifeImplModule());
to FooModule()
but then Animal.class
doesn't work, while Person.class
does.
Could anybody explain how does Guice private module work in terms of inheriting bindings? Is install(LifeImplModule())
in FooModule
not work for both Person.class
and child module BarModule()
?
question from:
https://stackoverflow.com/questions/65894636/guice-private-parent-module-expose-binding-to-child-modules 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…