簡體   English   中英

Guice requireBinding to TypeLiteral引發錯誤

[英]Guice requireBinding to TypeLiteral throws error

我有以下模塊,帶有requireBinding:

public class BillingModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(WalletBilling.class).to(WalletBillingService.class);
        requireBinding(Key.get(new TypeLiteral<Map<String, String>>() {}, ResourceUrls.class));
    }



    @Provides
    String returnUrls(@ResourceUrls Map<String, String> resourceUrls){
        return resourceUrls.get("hello");
    }

}

我注意到,當我運行實例化模塊的代碼時,出現以下錯誤:

線程“主要” com.google.inject.CreationException中的異常:Guice創建錯誤:

1) No implementation for java.util.Map<java.lang.String, java.lang.String> annotated with interface com.example.helloworld.annotations.ResourceUrls was bound.
  at com.example.helloworld.modules.BillingModule.configure(BillingModule.java:32)

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
    at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
    at com.google.inject.Guice.createInjector(Guice.java:95)
    at com.google.inject.Guice.createInjector(Guice.java:72)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at com.example.helloworld.Main.main(Main.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我以為通過用@ResourceUrls注釋@Provides方法參數,我已經滿足了綁定,但是事實並非如此,我是Guice的新手,所以會喜歡一些指針。

您需要以某種方式提供地圖,否則guice不知道從何處獲取地圖。 這樣的事情應該起作用。

@Provides
@ResourceUrls Map<String, String> getResourceUrls() {
    ...
    return map;
}

暫無
暫無

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

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