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.