PKuhn
PKuhn

Reputation: 1360

Guice specify modules to use for injection

In my application there are multiple modules binding something to a specific name or class. Is there a way to tell Guice, which modules it should use when resolving the dependencies to inject.

My simplified dependency graph looks something like this where blue indicates classes from module 1 and red indicates classes from module 2. Now I want to create two instances from the A class, but with different classes bound to some dependencies.

Dependencies

public class Module1 extends AbstractModule {
    @Override
    protected void configure() {
        bind(C.class).to(C_Impl1.class)
        bind(D.class).to(D_Impl1.class)
    }
}


public class Module2 extends AbstractModule {
    @Override
    protected void configure() {
        bind(C.class).to(C_Impl2.class)
        bind(D.class).to(D_Impl2.class)
    }
}

public class Application {
    @Inject @UseModules(Module1, ...) private final A someClassUsingImpl1;
    @Inject @UseModules(Module2, ...) private final A someClassUsingImpl2;

    public void doSomethingWithImpl1() {
        someClassUsingImpl1.doSomething() 
    }

    public void doSomethingWithImpl2() {
        someClassUsingImpl2.doSomething() 
    }
}

Upvotes: 2

Views: 1022

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95614

This is the problem private modules were built for. You will still need to use a binding annotation to differentiate whether you're asking for the Impl1 version of A or the Impl2 version of A.

/** Marks Impl1 classes. Inject @Impl1 A to get A using C_Impl1 and D_Impl1. */
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@interface Impl1 {}

/** Marks Impl2 classes. Inject @Impl2 A to get A using C_Impl2 and D_Impl2. */
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@interface Impl2 {}

/** This is now a PrivateModule. Only exposed bindings can be used outside. */
public class Module1 extends PrivateModule {
    @Override
    protected void configure() {
        // Bind C and D as you had before.
        bind(C.class).to(C_Impl1.class);
        bind(D.class).to(D_Impl1.class);
        // Here's the tricky part: You're binding "@Impl1 A" to
        // "A" without a binding annotation, but only in here.
        bind(A.class).annotatedWith(Impl1.class).to(A.class);
        // Now you expose @Impl1 A, so it can be used outside.
        // As long as A, C, and D are only bound within private modules,
        // they won't conflict with one another, and @Impl1 A is unique.
        expose(A.class).annotatedWith(Impl1.class);
    }
}

/** Treat Module2 the same way, as a private module. */

public class Application {
    @Inject @Impl1 private final A someClassUsingImpl1;
    @Inject @Impl2 private final A someClassUsingImpl2;
    // ...
}

If this is a common pattern for you, create a general PrivateModule that takes in the classes that vary as constructor parameters, so you don't need to repeat yourself. These can be added to the top-level injector, or installed within other modules.

Injector injector = Guice.createInjector(new YourMainModule(),
    new ImplModule(Impl1.class, C_Impl1.class, D_Impl1.class),
    new ImplModule(Impl2.class, C_Impl2.class, D_Impl2.class));

Upvotes: 1

Related Questions