如何从play guice injector

时间:2016-11-14 09:41:09

标签: playframework guice

我有一个测试,我在其中为带有命名注释的类型ICache<String, String>提供绑定:

public class UserControllerTest extends WithApplication {

    @Override
    protected Application provideApplication() {
        return new GuiceApplicationBuilder()
          .overrides(new AbstractModule() {
              @Override
              protected void configure() {
                  bind(new TypeLiteral<ICache<String, String>>() {
                  })
                    .annotatedWith(Names.named("SessionsCache"))
                    .toProvider(() -> new GuavaCache(1500, TimeUnit.MILLISECONDS));
              }
          }).build();
    }


    @Test
    public void someTest() throws Exception {
        final ICache<String, String> sessionCache = app.injector().instanceOf(
            new BindingKey<ICache>(ICache.class, Option.apply(new QualifierInstance(Names.named("SessionsCache"))))
        );
    }

}

可测试类( upd2 ):

public class UserController extends Controller {

    private final ICache<String,String> sessionsCache


    @Inject
    public UserController(@Named("SessionsCache") ICache<String,String> sessionsCache) {
       this.sessionsCache = sessionsCache;
    }
}

但它给了我错误:

No implementation for cache.ICache annotated with @com.google.inject.name.Named(value=SessionsCache) was bound.

我做错了什么?

更新

我发现discuss,该游戏不支持通用类型注入 - 这是guice功能。但问题仍然存在......

0 个答案:

没有答案