Thanks, Sam. I was not aware of the power of linked bindings. I think
documentation in this area could be improved. And please also document
that Multibindings cannot be overriden.

Reinhard

On 18 Jan., 18:40, Sam Berlin <[email protected]> wrote:
> In Guice 2.0, Modules.override didn't play nicely with MapBinder/Multibinder
> at all -- any attempt to use a Multibinder/MapBinder in the 'override
> module' broke everything.
>
> Guice 3.0 fixes this so that you *can* use MapBinder/Multibinder in the
> override module.  The behavior is limited to appending more info to the
> Map/Multibinder, though... it doesn't overwrite existing elements in the
> Map/Multibinder (mainly because it's not exactly clear how this would/should
> work).  You can emulate overriding the elements by using linked bindings and
> overriding the linked binding.  For example:
>    core module:
>          bind(Foo.class).toInstance(originalInstance);
>          mapBinder.addBinding("foo").to(Foo.class);
>
>  override module:
>          bind(Foo.class).toInstance(otherInstance);
>
> sam
>
> On Tue, Jan 18, 2011 at 2:27 AM, unguiculus <[email protected]>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 post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<google-guice%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.

-- 
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