Guice injector.getInstance抛出配置异常

时间:2017-01-07 18:14:40

标签: java dependency-injection guice

我正在使用提供程序创建Predicate实例。

@Provides
@Singleton
@Named("RecordFilters")
public Predicate<ImmutablePair<AbstractRecord, StreamRecord>> getAllFilters() {

    BackfillDataFilter backfillDataFilter = new BackfillDataFilter();
    DummyUpdateFilter dummyUpdateFilter = new DummyUpdateFilter();
    return input -> dummyUpdateFilter.test(input) && backfillDataFilter.test(input);
}

当我尝试使用injector.getInstance(Predicate.class)injector.getInstance(Key.get(Predicate.class, Names.Named("RecordFilters"))

获取其实例时

我得到了例外。

com.google.inject.ConfigurationException: Guice configuration errors:
  No implementation for java.util.function.Predicate was bound.
  while locating java.util.function.Predicate

请建议。

1 个答案:

答案 0 :(得分:2)

按照answer

中的说明进行尝试
injector.getInstance(Key.get(new TypeLiteral<Predicate<ImmutablePair<AbstractRecord, StreamRecord>>>(){})