Guice 根据父实例注释注入不同的实例



我在我的应用程序中使用了guice框架。我有一个场景,其中单个类可能需要相同接口 C 的多个实例(但用于不同的目的),如示例中所示。我正在尝试使用guice中的注释工具解决此问题。

如下面的示例所示,我希望 ConcreteImpl 的配置也由 guice 注入。但问题是 type1、type2 和 type3 实例的配置可能不同。假设我先验地拥有这些实例的配置,是否有一种工具可以根据请求配置的实例的上下文(用注释表示)注入它们?

    class A {
        @Inject
        public A(@Purpose1 C type1, @Purpose2 C type2, @Purpose3 C type3) {
        }
    }
    interface C {}
    class ConcreteImpl implements C {
        @Inject
        public ConcreteImpl(ConcreteImplConfig config) {}
    }
    class ConcreteImplConfig {
        String pty1;
        String pty2;
    }  

我的模块绑定是这样的——

    bind(C.class)
            .annotatedWith(Purpose1.class)
            .to(purpose1Cklass/**obtained from config**/);
    bind(C.class)
            .annotatedWith(Purpose2.class)
            .to(purpose2Cklass/**obtained from config**/);
    bind(C.class)
            .annotatedWith(Purpose3.class)
            .to(purpose3Cklass/**obtained from config**/);

这几乎就是我想做的

    bind(ConcreteImplConfig.class)
            .requestedThrough(Purpose1.class)
            .toInstance(purpose1config);
    bind(ConcreteImplConfig.class)
            .requestedThrough(Purpose2.class)
            .toInstance(purpose2config);
    bind(ConcreteImplConfig.class)
            .requestedThrough(Purpose3.class)
            .toInstance(purpose3config);

我已经看过辅助注入,它可以注入工厂,然后我们使用 factory.create(config),但我不倾向于这样做,因为合约往往会变得有点丑陋,而且我在应用程序开始时拥有所有配置,应该能够注入它们。

这是机器人腿问题。您需要为 C 创建一个私有模块。

abstract class CModule extends PrivateModule {
    private final Class<? extends Annotation> annotation;
    CModule(Class<? extends Annotation> annotation) {
        this.annotation = annotation;
    }
    @Override protected void configure() {
        bind(C.class).annotatedWith(annotation).to(C.class);
        expose(C.class).annotatedWith(annotation);
        bindConfig();
    }
    abstract void bindConfig();
}
public static void main(String[] args) {
        Injector injector = Guice.createInjector(
                new CModule(Propsal1.class) {
                    @Override void bindConfig() {
                        bind(ConcreteImplConfig.class).toInstance(new ConcreteImplConfig());
                    }
                },
                new CModule(Propsal2.class) {
                    @Override void bindConfig() {
                        bind(ConcreteImplConfig.class).toInstance(new ConcreteImplConfig());
                    }
                },
                new CModule(Propsal2.class) {
                    @Override void bindConfig() {
                        bind(ConcreteImplConfig.class).toInstance(new ConcreteImplConfig());
                    }
                }
                );
    }

最新更新