Guice requireBinding to TypeLiteral抛出错误

时间:2013-12-07 18:29:06

标签: java guice

我有以下模块,带有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");
    }

}

我注意到当我运行实例化模块的代码时,我收到以下错误:

线程“main”中的异常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方法param我已经满足了绑定,但事实并非如此,我是Guice的菜鸟,所以会很感激一些指示..

1 个答案:

答案 0 :(得分:1)

你需要以某种方式提供地图,否则guice不知道从哪里获取它。这样的事情应该有效。

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