Configuring several injections in Guice-Collection of common programming errors
You could use a binding annotation to let the user specify which one they wanted: http://code.google.com/p/google-guice/wiki/BindingAnnotations
You’d create the annotations like this:
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
public @interface WithClassA{}
@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
public @interface WithClassB{}
...
Then your user’s constructor looks like
@Inject
public MyConstructor(@WithClassA Panel thePanel) {...}
Then when you do the binding, you would use .annotatedWith:
bind(Panel.class)
.annotatedWith(WithClassA.class)
.toProvider(ProviderThatReturnsPanelConstructedWithA.class);
bind(Panel.class)
.annotatedWith(WithClassB.class)
.toProvider(ProviderThatReturnsPanelConstructedWithB.class);
Just in case, here’s the documentation on how to set up providers: http://code.google.com/p/google-guice/wiki/ProviderBindings