13

到目前为止,我成功使用了 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 不喜欢工厂方法中的泛型参数,或者我对辅助注入工厂的普遍误解?谢谢!

4

1 回答 1

12

上面的代码有两个问题。

首先,RealPayment实现Payment<SwissFrancs>,但PaymentFactory.create返回Payment<Currency>。APayment<SwissFrancs>不能从返回 的方法中返回Payment<Currency>。如果您将返回类型更改为createto Payment<? extends Currency>,那么RealPayment将起作用(因为它是Payment用于扩展的东西Currency)。

其次,您implement确实需要使用以 aTypeLiteral作为其第一个参数的版本。这样做的方法是使用匿名内部类。要表示“付款”,您可以使用

new TypeLiteral<Payment<? extends Currency>>() {}

有关更多信息,请参阅该TypeLiteral构造函数的 Javadoc。

于 2011-04-20T21:55:54.010 回答