2
什麼即時試圖做的是創建地圖,其中關鍵是類擴展我的抽象類如何在GinMapBinder中正確使用TypeLiteral?
GinMapBinder<Class<? extends Key>, Value> mapBinder = GinMapBinder
.newMapBinder(binder(),
new TypeLiteral<Class<? extends Key>>() {
}, new TypeLiteral<Value>() {
});
,但是當我試圖填充我的地圖
mapBinder.addBinding(KeyImpl.class).to(Value.class);
我收到提示:
Error injecting @com.google.gwt.inject.client.multibindings.Internal() java.lang.Class<? extends my.test.gwt.gin.objects.Key>: Unable to create or inherit binding: No implementation bound for '@com.google.gwt.inject.client.multibindings.Internal() java.lang.Class<? extendsmy.test.gwt.gin.objects.Key>' and an implicit binding cannot be created because the type is annotated.
Path to required node:
@com.google.gwt.inject.client.multibindings.Internal com.google.gwt.inject.client.multibindings.MapEntry<java.lang.Class<? extends my.test.gwt.gin.objects.Key>, my.test.gwt.gin.objects.Value> [com.google.gwt.inject.client.multibindings.BindingRecorder.bind(BindingRecorder.java:42)]
-> com.google.gwt.inject.client.multibindings.MapEntry<java.lang.Class<? extends my.test.gwt.gin.objects.Key>, my.test.gwt.gin.objects.Value> [com.google.gwt.inject.client.multibindings.BindingRecorder.bind(BindingRecorder.java:42)]
-> @com.google.gwt.inject.client.multibindings.Internal() java.lang.Class<? extends my.test.gwt.gin.objects.Key> [@Inject constructor of com.google.gwt.inject.client.multibindings.MapEntry<java.lang.Class<? extends my.test.gwt.gin.objects.Key>, my.test.gwt.gin.objects.Value>]
如果我不會用TypeLiteral
這將工作,但我不想與原始類型的類。 所以如果有人能幫我解決這個問題,我會很高興。 在此先感謝
好奇你是如何獲得新的TypeLiteral映射上班。我使用guice 3和杜松子酒2.1.1。 TypeLiteral具有受保護的構造函數。我無法弄清楚如何使用泛型類創建類型文字,就像您所做的那樣。 –
@ChrisHinshaw如果您將'TypeLiteral'初始化爲匿名類,那麼您可以訪問它的構造函數 – user902383