How to inject a generified factory for dependent class?

11 views
Skip to first unread message

Alex Luya

unread,
Aug 11, 2011, 8:19:17 PM8/11/11
to google-guice
I have three classes:

This brief representation of their relationships:
C-->B-->A
-------------------------------------------------
("-->" means depending on,A and B are generic classes)

Below is detailed one:
------------------------------------------------------

class A<M,I>{
@Inject
A(Factory<M,I> factory){
......
}
}

class B depends on class A
--------------------------------
class B<M,I>{
@Inject
B(A<M,I> a){
......
}
}

class C depends on class B
-------------------------------------

class CImpl implement C{
@Inject
B(B<MExt,IExt> a){
//here, MExt and IExt come from interface C
}
}

---------------------------------------------
Question is how to inject factory for class A?

Fred

unread,
Aug 17, 2011, 9:03:08 PM8/17/11
to google...@googlegroups.com
Work backwards:

CImpl needs B<MExt, IExt>
B<MExt, IExt> needs A<MExt, IExt>
A<MExt, IExt> needs Factory<MExt, IExt>

bind(new TypeLiteral<Factory<MExt, IExt>(){})
  .to(.....);

Fred
Reply all
Reply to author
Forward
0 new messages