Multibinders and PrivateModules

2,017 views
Skip to first unread message

Árni Hermann

unread,
Mar 3, 2009, 5:00:35 PM3/3/09
to google-guice
Hi

Do you guys know if it's possible to expose a multibinder which is
bound in a private module?

I'm talking about something like the following...

In private module 1:
Multibinder<Source> multibinder = Multibinder.newSetBinder(binder(),
Source.class);
multibinder.addBinding().to(SourceImpl.class);
expose(??);

In private module 2:
Multibinder<Source> multibinder = Multibinder.newSetBinder(binder(),
Source.class);
multibinder.addBinding().to(SourceImpl.class);
expose(??);

In public module:
Multibinder<Source> multibinder = Multibinder.newSetBinder(binder(),
Source.class);
multibinder.addBinding().to(SourceImpl3.class);

With an injector which uses all of the moduels above, what would I
need to expose() in the private modules to be able to have Set<Source>
injected in a class which is bound in the public module?

Regards,
Árni Hermann

limpb...@gmail.com

unread,
Mar 3, 2009, 9:21:45 PM3/3/09
to google-guice
Regretfully, multibindings cannot span injectors and therefore they
cannot span private modules. This sucks! But there's a (lame)
workaround. Expose a key in the private module, and multibind it in a
regular module:

Injector injector = Guice.createInjector(
new PrivateModule() {
public void configure() {
bind(Key.get(Source.class, named("one"))).to
(SourceImpl.class);
expose(Key.get(Source.class, named("one")));
...
}
},
new PrivateModule() {
public void configure() {
bind(Key.get(Source.class, named("two"))).to
(SourceImpl.class);
expose(Key.get(Source.class, named("two")));
...
}
},
new Module() {
public void configure() {
Multibinder<Source> multibinder
= Multibinder.newSetBinder(binder(), Source.class);
multibinder.addBinding().to(Key.get(Source.class, named
("one")));
multibinder.addBinding().to(Key.get(Source.class, named
("two")));
}
});

Árni Hermann

unread,
Mar 3, 2009, 9:33:27 PM3/3/09
to google-guice
Yup, this sucks! :)

This (lame) workaround is simpler than the one I had in mind and looks
like it works just fine for my use case.

Thanks for the quick reply.

Regards,
Árni Hermann

Matthew Madson

unread,
Feb 8, 2016, 12:53:40 PM2/8/16
to google-guice
I don't suppose this has gotten less lame over the years?

Jonathan Leitschuh

unread,
Jun 7, 2019, 4:52:03 PM6/7/19
to google-guice
I've tried to offer a better solution that uses Kotlin. So maybe it has gotten better over the years finally?
Reply all
Reply to author
Forward
0 new messages