2011-04-01 56 views
12

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

+0

我想知道你是否应该将'PaymentLlassral >'而不是'Payment.class'传递给'implement'? – Jeremy 2011-04-01 14:21:38

+0

@Jeremy Heiler谢谢,但你会怎么做? 'TypeLiteral'没有公共构造函数,如果使用'TypeLiteral.get(Payment.class)',你会得到相同的异常。 – kraftan 2011-04-01 18:50:21

+0

也许这样?'TypeLiteral.get(Types.newParameterizedType(Payment.class,Currency.class));' – Jeremy 2011-04-01 18:54:09

回答

11

上面的代码有两个问题。

首先,RealPayment执行Payment<SwissFrancs>,但PaymentFactory.create返回Payment<Currency>。 A Payment<SwissFrancs>不能从返回Payment<Currency>的方法返回。如果您将退货类型create更改为Payment<? extends Currency>,那么RealPayment将起作用(因为它是Payment,用于延伸Currency的某些内容)。

其次,您需要使用implement版本,它将TypeLiteral作为其第一个参数。做到这一点的方法是使用匿名内部类。为了表示'支付”,你可以使用

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

见该TypeLiteral构造的Javadoc以获取更多信息。