到目前为止,我成功地使用了google guice 2.在迁移到guice 3.0时,我遇到了辅助注入工厂的麻烦。假设下面的代码guice辅助注入工厂中的通用返回类型的问题
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)
随着辅助注射创作者从吉斯2我的配置工作:
bind(PaymentFactory.class).toProvider(
FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));
唯一我发现迄今为止的解决方法是从工厂方法的返回类型中删除通用参数:
public interface PaymentFactory {
Payment create(Date date);
}
有谁知道,为什么guice 3不喜欢工厂方法中的泛型参数或我通常误解了辅助注入工厂?谢谢!
我想知道你是否应该将'PaymentLlassral>'而不是'Payment.class'传递给'implement'? –
Jeremy
2011-04-01 14:21:38
@Jeremy Heiler谢谢,但你会怎么做? 'TypeLiteral'没有公共构造函数,如果使用'TypeLiteral.get(Payment.class)',你会得到相同的异常。 – kraftan 2011-04-01 18:50:21
也许这样?'TypeLiteral.get(Types.newParameterizedType(Payment.class,Currency.class));' – Jeremy 2011-04-01 18:54:09