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()
?
Since BarModule
is a private module, any modules installed inside it won't be available externally.
That's why
install(new LifeImplModule());
doesn't grant access to Life
binding for FooModule
.
You can solve it by explicitly exposing Life
from BarModule
install(new LifeImplModule());
expose(Life.class);