If you only want to add server to the MapBinder then there is no need to
overwrite modules.
From the JavaDoc [1]:
Contributing mapbindings from different modules is supported. For
example, it is okay to have both |CandyModule| and |ChipsModule| both
create their own |MapBinder<String, Snack>|, and to each contribute
bindings to the snacks map. When that map is injected, it will contain
entries from both modules.
[1]
http://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/multibindings/MapBinder.html
On 17.01.2017 00:52, Rchytas wrote:
I was wondering where does override go (Sorry if its a dumb question,
I'm new to Guice). I've module deployed in web app as below -
import com.google.inject.multibindings.MapBinder;
public class ExternalDatabaseModule extends AbstractModule implements
com.abc.Module {
public void configure() {
MapBinder<String, ExternalDatabaseConnection> m =
MapBinder.newMapBinder(binder(), String.class,
ExternalDatabaseConnection.class);
m.addBinding("DBServer1).to(ExternalDBServer1Connection.class);
m.addBinding("DBServer2").to(ExternalDBServer2Connection.class);
}
}
I then created another module and exported this to a jar file and then
added this jar to tomcat/web-inf/lib. But my new binding did not get
discovered
public class ABCExternalDatabaseModule extends AbstractModule implements
com.abc.Module {
@Override
public void configure() {
MapBinder<String, ExternalDatabaseConnection> m = MapBinder
.newMapBinder(binder(), String.class,
ExternalDatabaseConnection.class);
m.addBinding("DBServer3").to(ExternalDBServer3Connection.class);
}
public ABCExternalDatabaseModule() {
Guice.createInjector(Modules.override(
new ExternalDatabaseModule()).with(
new ABCExternalDatabaseModule()));
}
}
On Monday, January 17, 2011 at 11:27:39 PM UTC-8, Reinhard Nägele wrote:
I just discovered that bindings created with a MapBinder cannot be
overridden. Here's an example:
Module module1 = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> mapBinder =
MapBinder.newMapBinder(binder(), String.class,
String.class);
mapBinder.addBinding("foo").toInstance("foo1");
}
};
Module module2 = new AbstractModule() {
@Override
protected void configure() {
MapBinder<String, String> mapBinder =
MapBinder.newMapBinder(binder(), String.class,
String.class);
mapBinder.addBinding("foo").toInstance("foo2");
}
};
Guice.createInjector(Modules.override(module1).with(module2));
Executing the above code yields the following error in Guice 2.0 and
3.0-rc2:
Exception in thread "main" com.google.inject.CreationException: Guice
creation errors:
1) Map injection failed due to duplicated key "foo"
at com.google.inject.multibindings.MapBinder$RealMapBinder
$1.initialize(MapBinder.java:355)
at spielplatz.TestApp$2.configure(TestApp.java:31)
1 error
at
com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:
416)
at
com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:
175)
at
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:
109)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at spielplatz.TestApp.main(TestApp.java:36)
Is this intended behavior? It looks like a bug to me, or am I missing
something?
Reinhard
--
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 [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/7d227d88-812a-4abe-92e7-77e79d2f644c%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/7d227d88-812a-4abe-92e7-77e79d2f644c%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/7c5e1e12-eabd-59a5-c01e-d8826742d8ad%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.