Search code examples
guice

Guice inject resolution with multiple implementation to type


i have a situation where there two bindings with two different annotations for a type available

bind(MyType.class).annotatedWith(X.class).toInstance(XMyTypeImpl.class) bind(MyType.class).annotatedWith(Y.class).toInstance(YMyTypeImpl.class)

I have another class which has dependency on MyType @Inject public Upstream(MyType myType) {}

without having annotation inside Upstream class, is it possible to get one of implementations of MyType.class get injected?

During bind, can i suggest to guice one of them is default for injection ?


Solution

  • Simply add additional binding without annotation for your desired target value, e.g.:

    bind(MyType.class).to(XMyTypeImpl.class)