到目前为止,我成功使用了 google guice 2。在迁移到 guice 3.0 时,我遇到了辅助注入工厂的问题。假设以下代码
public interface Currency {}
public class SwissFrancs implements Currency {}
public interface Payment<T extends Currency> {}
public class RealPayment implements Payment<SwissFrancs> {
@Inject
RealPayment(@Assisted Date date) {}
}
public interface PaymentFactory {
Payment<Currency> create(Date date);
}
public SwissFrancPaymentModule extends AbstractModule {
protected void configure() {
install(new FactoryModuleBuilder()
.implement(Payment.class, RealPayment.class)
.build(PaymentFactory.class));
}
}
在创建注入器时,我得到以下异常:
com.google.inject.CreationException: Guice creation errors:
1) Payment<Currency> is an interface, not a concrete class.
Unable to create AssistedInject factory. while locating Payment<Currency>
at PaymentFactory.create(PaymentFactory.java:1)
使用来自 guice 2 的辅助注入创建器,我的配置工作:
bind(PaymentFactory.class).toProvider(
FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));
到目前为止,我发现的唯一解决方法是从工厂方法的返回类型中删除泛型参数:
public interface PaymentFactory {
Payment create(Date date);
}
有谁知道,为什么 guice 3 不喜欢工厂方法中的泛型参数,或者我对辅助注入工厂的普遍误解?谢谢!