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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to