【问题标题】:Guice Assisted Inject with multiple implementations of interface具有多种接口实现的 Guice 辅助注入
【发布时间】:2014-07-25 06:10:58
【问题描述】:

我有一个接口 A,它有多个实现,比如 B、C 和 D。 我正在使用 guice 辅助注入来创建这些类。类 B 和 C 在构造函数中使用相同的辅助参数(例如 b、c),而 D 具有一个不同的参数(例如 d)。 我实现这一点的方式是有一个工厂 AFactory 有两种方法: create(int b, int c) 和 createD(int d) 在我的模块中,我创建了一个 PrivateModule 来将具体类绑定到工厂。

代码如下所示:

public static PrivateModule getAFactoryModule(final String factoryBindingKey,final Class<? extends A> targetClassToCreate) {
    return new PrivateModule() {

        @Override
        public void configure() {
            install(new FactoryModuleBuilder().implement(
                    A.class, targetClassToCreate).build(
                    AFactory.class));

            bind(AFactory.class).annotatedWith(Names.named(factoryBindingKey)).to(
                    Key.get(AFactory.class));
            expose(Key.get(AFactory.class, Names.named(factoryBindingKey)));
        }
    };
}

我这样调用 PrivateModule:

install(getAFactoryModule("B", B.class));
install(getAFactoryModule("C", C.class));
install(getAFactoryModule("D", D.class));

但是,这给出了一个错误提示:

com.guice.test.B has @AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject

com.guice.test.C has @AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject

com.guice.test.D has @AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.create(). Unable to create assisted inject

Guice 似乎正在尝试使用与预期不同的创建方法。知道如何解决这个问题吗?任何指针将不胜感激!

谢谢!

【问题讨论】:

    标签: java dependency-injection guice


    【解决方案1】:

    听起来你在做一些不安全的事情——让你的一些工厂方法没有解决——而 Guice 很困惑。基本上,FactoryModuleBuilder 正在生成以下代码:

    @Named("B") public class GeneratedBClass implements AFactory {
      A create(int b, int c) { return new B(...); }
      A create(int d) { /* no implementation! */ }
    }
    @Named("C") public class GeneratedBClass implements AFactory {
      A create(int b, int c) { return new C(...); }
      A create(int d) { /* no implementation! */ }
    }
    @Named("D") public class GeneratedBClass implements AFactory {
      A create(int b, int c) { /* no implementation! */ }
      A create(int d) { return new D(...); }
    }
    

    这三个缺失的实现是 Guice 抱怨的原因:Guice 无法为这些实现找到一个好的实现,并且您希望它们连接起来。但是,除了 Guice,您还设计了一个非常危险的 API:根据您的配置,您的一种方法可以工作,而另一种方法不能工作——这比 DI 的设计目的更危险。

    我会将您的配置切换为仅切换“B”和“C”,并让它们都暴露“D”:

    public interface AFactory {
      @Named("BorC") A create(int B, int C);
      @Named("D") A create(int D);
    }
    
    install(new FactoryModuleBuilder()
        .implement(A.class, Names.named("BorC"), targetClassToCreate)
        .implement(D.class, Names.named("D"), D.class)
        .build(AFactory.class));
    

    ...或者,因为您应该在编译时知道是要调用二参数还是一参数构造函数,只需将 D 切换到它自己的工厂:

    public interface DFactory {
      D create(int D);
    }
    
    // If DFactory creates an A, you need implement(). Otherwise, this works.
    install(new FactoryModuleBuilder().build(DFactory.class));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多