Multiple implementations

76 views
Skip to first unread message

jh24

unread,
Sep 2, 2015, 8:33:57 AM9/2/15
to google-guice
Hi,

Suppose the following classes/interfaces are given:

interface IA { }

public final class A implements IA {
   IB b;
   @Inject
   public A(IB b) { this.b = b; }
}

public final class A2 implements IA { }

interface IB { }

public final class B implements IB {
   IA a;
   @Inject
   public B(IA a) { this.a = a; }
}

public final class B2 implements IB { }

Ie. there are two implementations of IB, one of which depends on IA, and there are two implementations of IA, one of which depends on IB.

Is it possible to create a single injector for which:

  (a) injector.getInstance(IA.class) returns an instance of A whose field 'b' is an instance of B2, 
  (b) injector.getInstance(IB.class) returns an instance of B whose field 'a' is an instance of A2, and 
  (c) no extra annotations are added to the above classes.

Apologies if this actually has a straightforward solution.  Examples appreciated.

Stephan Classen

unread,
Sep 2, 2015, 9:28:28 AM9/2/15
to google...@googlegroups.com
No, I this is not possible. The injector injects the same class independent of the target. This means if there are no additional annotations it will always inject either A1 or A2 (depends on the configuration in the module) for IA. The same holds for the Bs.
Reply all
Reply to author
Forward
0 new messages