Hello,
I am using Guice 3.0 to develop an Application that consists of several packages.
I would like to have Bindings which are only visible in one package and its subpackages.
I have one Module per Package where I configure the Bindings used in that package.
Currently I the Module in the root package installs the Modules in its subpackages, which then install the Modules in their subpackages and so on.
That means if I have the following packages:
Application
Application.ComponentA
Application.ComponentB
Application.ComponentB.SubcomponentC
I would have the following Modules:
Application.ApplicationModule
Application.ComponentA.AModule
Application.ComponentB.BModule
Application.ComponentB.SubcomponentC.CModule
The ApplicationModule would look like:
public class ApplicationModule extends AbstractModule{
@Override
protected void configure() {
install(new AModule());
install(new BModule());
// Configure some Bindings
}
}
And I would create one Injector in the Main Method using:
Guice.createInjector(new MainModule());
Objects are created using Constructor and Method injection, injecting Providers if needed.
However, if BModule maps List<String> to ArrayList<String> then AModule also sees that Binding. Also if AModule tries to bind List<String> to LinkedList<String> I will get a conflict.
I would like to have a binding configured in BModule to be only visible within ComponentB and SubcomponentC. Such that a class which requests a List<String> gets a LinkedList if its in ComponentA, a ArrayList if its in B or C and an error if its somewhere else.
The reason is, that I would like packages on the same level to be independent and only depend on their ancestors.
I have seen that Guice features child-injectors and PrivateModule, but I am unsure if/how I could use that to achieve my goal.
Thanks in advance,
Martin Haug