Discussion:
Module overrides don't work with MapBinder
Rchytas
2017-01-16 23:52:14 UTC
Permalink
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()));
}

}
I just discovered that bindings created with a MapBinder cannot be
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
Exception in thread "main" com.google.inject.CreationException: Guice
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
416)
at
175)
at
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 google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2017-01-17 00:19:10 UTC
Permalink
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
Post by Rchytas
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()));
}
}
I just discovered that bindings created with a MapBinder cannot be
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
Exception in thread "main" com.google.inject.CreationException: Guice
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
416)
at
175)
at
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
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 google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
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.
Rchytas
2017-01-17 17:39:43 UTC
Permalink
Thanks
Post by Stephan Classen
If you only want to add server to the MapBinder then there is no need to
overwrite modules.
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
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()));
}
}
I just discovered that bindings created with a MapBinder cannot be
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
Exception in thread "main" com.google.inject.CreationException: Guice
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
416)
at
175)
at
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
<javascript:>.
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?utm_medium=email&utm_source=footer>
https://groups.google.com/d/msgid/google-guice/7d227d88-812a-4abe-92e7-77e79d2f644c%40googlegroups.com
.
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 google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
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/239a0f19-057b-462a-b39e-f8777e90b72b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...