小编kra*_*tan的帖子

guice辅助注射工厂中通用返回类型的问题

到目前为止,我成功地使用了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));
    }
}
Run Code Online (Sandbox Code Playgroud)

在创建注入器时,我得到以下异常:

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) …
Run Code Online (Sandbox Code Playgroud)

java guice guice-3

13
推荐指数
1
解决办法
3278
查看次数

Java foreach声明中的字段赋值

我知道以下示例中使用的foreach循环无法编译.但有人知道为什么不允许在foreach循环声明中使用字段?

public class Foo {
    private Object obj;

    public void run(List<Object> objects) {
        for (obj : objects) {
            process();
        }
    }

    private void process() {
        // do something with obj
    }
}
Run Code Online (Sandbox Code Playgroud)

java foreach

3
推荐指数
1
解决办法
1103
查看次数

标签 统计

java ×2

foreach ×1

guice ×1

guice-3 ×1