簡體   English   中英

Guice-沒有實現綁定(對於List <String> )

[英]Guice - No implementation was bound (for List<String>)

我正在嘗試使用特定注釋對List進行注釋時綁定List的實例。

我嘗試使用實例綁定和提供程序方法,但始終收到錯誤消息。

這是我的@Provides方法和configure()

@Provides @Named("Regions")
public List<String> getRegions() {
    return AppConfig.findVector("Regions"); //this would return a Vector<String>
}

@Override
protected void configure() {
    bind(List.class).annotatedWith(Names.named("Regions")).to(Vector.class);
}

這是我嘗試獲取實例的方法-

List<String> regions = injector.getInstance(Key.get(List.class, Names.named("Regions")));

這是我得到的錯誤

com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for java.util.List annotated with @com.google.inject.name.Named(value=Regions) was bound.
while locating java.util.List annotated with @com.google.inject.name.Named(value=Regions)

對於Guice來說, List不同於List<String> ,並且@Named("Regions") List不同於@Named("Regions") List<String>

使用new TypeLiteral<List<String>>() {} (NB一般類型的匿名內部類,因為這是需要在運行時獲得非擦除仿制葯)在Key.get請求從吉斯泛型類型。

另外,您的bind語句無濟於事,可以將其刪除。 您需要在@Provides方法中手動將Vector轉換為List,但是bind並不能幫助您做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM