Limit visibility of Bindings to Package and Subpackage

214 views
Skip to first unread message

MrIcehawk

unread,
Nov 20, 2014, 4:36:19 AM11/20/14
to google...@googlegroups.com
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

nino martinez wael

unread,
Dec 4, 2014, 4:42:48 AM12/4/14
to google-guice
use a childinjector with private modules for this

Application.ApplicationModule <--- parent injector
Application.ComponentA.AModule <-- ChildA (private module)
Application.ComponentB.BModule <-- ChildB (private module)
Application.ComponentB.SubcomponentC.CModule <--ChildB (Installed from BModule?)

2014-11-20 10:36 GMT+01:00 'MrIcehawk' via google-guice
<google...@googlegroups.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to google-guice...@googlegroups.com.
> To post to this group, send email to google...@googlegroups.com.
> Visit this group at http://groups.google.com/group/google-guice.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-guice/d2917f17-550f-4334-b28a-883da852e14d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Best regards / Med venlig hilsen
Nino Martinez
Reply all
Reply to author
Forward
0 new messages