Hi Jozsef,
You need to use a PrivateBinder to isolate the bindings. Bind b1, a1, and
a2 in using one private binder. Use another private binder to bind b2 and
a3. And finally, expose a1, a2, and a3. Attached is an example :)
On Friday, August 16, 2013 7:39:58 AM UTC-7, Jozsef Hegedus wrote:
>
> Hi,
>
> I have read (but probably not completely yet understood) Dhanji's book and
> I don't know how to solve the following simple problem:
>
> I have class A that uses(depends on) class B. B has no dependency.
> Now I want to create
> three different A objects : a1, a2 and a3;
> and two different B objects : b1 and b2
> in such a way that
> 1) a1 and a2 are injected with b1
> 2) a3 is injected with b2.
>
> Any suggestions?
>
> Cheers
>
> Jozsef
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.
import com.google.inject.Inject;
public class ClassA {
private ClassB classB;
@Inject
public ClassA(ClassB classB) {
this.classB = classB;
}
public ClassB getClassB() {
return classB;
}
@Override
public String toString() {
return "ClassA [classB=" + classB + "]";
}
}
import com.google.inject.Inject;
public class ClassB {
private String id;
@Inject
public ClassB(String id) {
this.id = id;
}
public String getId() {
return id;
}
@Override
public String toString() {
return "ClassB [id=" + id + "]";
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.PrivateBinder;
import com.google.inject.name.Names;
public class Guicer {
private static final Key<ClassA> A1 = Key.get(ClassA.class, Names.named("a1"));
private static final Key<ClassA> A2 = Key.get(ClassA.class, Names.named("a2"));
private static final Key<ClassA> A3 = Key.get(ClassA.class, Names.named("a3"));
public static void main(String...args) {
Injector i = Guice.createInjector(new MyModule());
System.out.println(i.getInstance(A1));
System.out.println(i.getInstance(A2));
System.out.println(i.getInstance(A3));
}
private static class MyModule extends AbstractModule {
@Override
protected void configure() {
bindingProblem1("b1",binder().newPrivateBinder());
bindingProblem2("b2",binder().newPrivateBinder());
}
private void bindingProblem1(String id, PrivateBinder binder) {
// 1) a1 and a2 are injected with b1
binder.bind(String.class).toInstance(id);
binder.bind(ClassB.class);
binder.bind(A1).to(ClassA.class);
binder.bind(A2).to(ClassA.class);
binder.expose(A1);
binder.expose(A2);
}
private void bindingProblem2(String id, PrivateBinder binder) {
// a3 is injected with b2
binder.bind(String.class).toInstance(id);
binder.bind(ClassB.class);
binder.bind(A3).to(ClassA.class);
binder.expose(A3);
}
}
}