Unfortunately, an annotation isn't an option for me.

The interface solution is working well, but I'll look into
childInjectors too.

Thanks!
Christopher

On Oct 7, 2:36 pm, Alen Vrečko <[email protected]> wrote:
> Sure, if you absolutely don't want to use a binding annotation then go
> for the interface.
>
> If you absolutely don't want to go for the interface then you can use
> childInjectors.
>
> Cheers
> Alen
>
> On Oct 7, 10:46 pm, Christopher Berner <[email protected]>
> wrote:
>
>
>
>
>
>
>
> > Ah, I see. So one way to fix this seems to be to make Outer an
> > interface, and then bind an implementation of it. Would that be the
> > correct way of fixing this, or is there a better way?
>
> > Thanks!
> > Christopher
>
> > On Oct 7, 12:59 pm, Alen Vrečko <[email protected]> wrote:
>
> > > Be careful when exposing bindings without annotation!!!
>
> > > Module1: expose(Outer.class) -> Outer.class is now "global" and not
> > > "private"
> > > Module2:
> > > bind(Outer.class).annotatedWith(Names.named("2")).to(Outer.class)
> > > -> the to(Outer.class) actually binds to the exposed binding from
> > > module1!
>
> > > Note: If you do bind(Outer.class) in Module2 you will get an error.
>
> > > Cheers
> > > Alen
>
> > > On Oct 7, 8:00 pm, Christopher Berner <[email protected]>
> > > wrote:
>
> > > > I have a class A that depends on an implementation of class B, and I'm
> > > > trying to create two private modules that expose a binding for class
> > > > A, but with different bindings for the class B. However, even though I
> > > > exposed one binding with annotation and one without, I'm still getting
> > > > the same implementation of class B in both cases.
>
> > > > I've included some code that demonstrates the problem, if anyone can
> > > > point out what I'm doing wrong I would be very grateful!
>
> > > > import org.junit.Assert;
>
> > > > import com.google.inject.Guice;
> > > > import com.google.inject.Inject;
> > > > import com.google.inject.Injector;
> > > > import com.google.inject.Key;
> > > > import com.google.inject.PrivateModule;
> > > > import com.google.inject.name.Names;
>
> > > > public class Junk {
> > > >         public static void main(String[] args) {
> > > >                 Injector injector = Guice.createInjector(new Module2(), 
> > > > new
> > > > Module1());
>
> > > >                 Outer outer2 = injector.getInstance(Key.get(Outer.class,
> > > > Names.named("2")));
> > > >                 Outer outer1 = injector.getInstance(Outer.class);
>
> > > >                 Assert.assertTrue(outer1.inner instanceof Inner1);
> > > >                 Assert.assertTrue("Expected Inner2, but got " +
> > > > outer2.inner.getClass(), outer2.inner instanceof Inner2);
> > > >         }
>
> > > > }
>
> > > > interface Inner {}
> > > > class Inner1 implements Inner {};
> > > > class Inner2 implements Inner {};
>
> > > > class Outer {
> > > >         Inner inner;
>
> > > >         @Inject
> > > >         Outer(Inner inner) {
> > > >                 this.inner = inner;
> > > >         }
>
> > > > }
>
> > > > class Module1 extends PrivateModule {
> > > >         @Override
> > > >         protected void configure() {
> > > >                 binder().requireExplicitBindings();
> > > >                 bind(Outer.class);
> > > >                 expose(Outer.class);
> > > >                 bind(Inner.class).to(Inner1.class);
> > > >         }
>
> > > > }
>
> > > > class Module2 extends PrivateModule {
> > > >         @Override
> > > >         protected void configure() {
> > > >                 binder().requireExplicitBindings();
> > > >                 
> > > > bind(Outer.class).annotatedWith(Names.named("2")).to(Outer.class);
> > > >                 expose(Outer.class).annotatedWith(Names.named("2"));
> > > >                 bind(Inner.class).to(Inner2.class);
> > > >         }
>
> > > > }

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