The best solution I think would be to move away from member injection and
use constructor injection:
public abstract class A {
private final MyInterface myInterface;
protected A(MyInterface myInterface) {
...
}
}
class SubA extends A {
@Inject SubA(MySubInterface mySubInterface) {
super(mySubInterface);
}
...
}
or if you don't want to specify the subtypes in the class, you can use an
annotation:
class SubA extends A {
@Inject SubA(@SomeDescriptivePhrase MyInterface myInterface) {
super(mySubInterface);
}
...
}
These solutions are clear because the binding can be determined statically
(by human eyes).
More dynamic alternatives include use of scope and custom injections, but
those I think would do more harm to the clarity of the program than good to
the programmer.
Fred
On Wed, Apr 6, 2011 at 1:49 AM, qu <[email protected]> wrote:
> Hi all!
>
> I am currently trying to refactor a project that is still in an early
> phase of development to use Guice. The problem i am facing and for
> which i havent found any solution searching this group is:
>
> I need to inject implementations of interfaces, depending on the
> concrete class these interfaces are members of.
> For example:
>
> public abstract class A {
> @Inject("impl dependent on the A's subtype")
> private IMyInterface myInterface;
> }
>
> I have several classes derived from A, depending on this very subclass
> of A, i need to chose which implementation
> of IMyInterface to choose for injection.
> Is there any way to this with Guice?
>
> Best regards,
>
> --qu
>
> --
> 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.
>
>
--
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.