2015-11-20 83 views
1

我正在嘗試使用Jersey和HK2。我需要真正的古怪的類型綁定:HK2 TypeLiteral和通配符

List<TransformationService<? extends Transformation, ? extends TransformationInfor>>

所以,我有我的粘結劑定義如下:

resourceConfig.register(new AbstractBinder() { 
     @Override 
     protected void configure() { 
      List<TransformationService<? extends Transformation, ? extends TransformationInfo>> transformationServices = ... ; 

      bind(transformationServices) 
        .to(new TypeLiteral<List<TransformationService<? extends Transformation, ? extends TransformationInfo>>>() {}); 

      // This class needs the list for its construction 
      bind(TransformationServiceImpl.class).to(TransformationService.class); 
     } 
    }); 

當我運行的代碼,雖然我得到不同的是我的名單不能注入(包裝ommitted):

[11/20/15 16:46:34] WARNING org.glassfish.jersey.internal.Errors logErrors : The following warnings have been detected: WARNING: Unknown HK2 failure detected: 
MultiException stack 1 of 3 
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=List<TransformationService<? extends ...Transformation,? extends ...TransformationInfo>>,parent=TransformationServiceImpl,qualifiers={},position=3,optional=false,self=false,unqualified=null,334434299) 
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74) 
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:214) 

有關如何注入這樣的怪人與HK2的任何提示?

+0

我認爲這是在這裏找到答案:http://stackoverflow.com/questions/23992714/how-to-inject-an-unbound-generic-type-in​​-hk2 –

回答

1

據我瞭解HK2注射規則是CDI(see spec

同樣在某些時候,它提到:

然而,一些Java類型是不合法的bean類型:

  • 類型變量不是合法的bean類型。
  • 包含通配符類型參數的參數化類型不是合法的bean類型。
  • 其組件類型不是合法bean類型的數組類型。

我認爲,在我的例子中,我試圖創造TypeLiteral包含通配符參數化類型。

無論如何,在我的情況下,我刪除了無界的類型,它的工作原理。所需要的變化是:

bind(transformationServices) 
    .to(new TypeLiteral<List<TransformationService>>() {}); 
+0

這是確實,hk2使用與CDI使用的類型安全相同的規則。我必須深入探討爲什麼你的特定用例不起作用,這些規則有點毛骨悚然 – jwells131313