Rchytas
2017-01-16 23:52:14 UTC
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()));
}
}
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
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.
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.