java - Guice requireBinding 到 TypeLiteral 抛出错误

标签 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");
    }

}

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

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

最佳答案

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

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

关于java - Guice requireBinding 到 TypeLiteral 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444935/

相关文章:

java - 应用服务器和JVM是什么关系?

java - 错误: Could not find or load main class - . jar文件执行

java - 泛型:创建参数化类参数

java - 使用 Selenium Web Driver 和 Java 切换到第一个选项卡

java - Guice - 如何提供一个 API,支持灵活的可插拔策略实现及其自己的绑定(bind)?

java - javax.inject.Inject 和 com.google.inject.Inject 有什么区别?

java - 如何查找数组中重复的元素?

gwt - 工厂 "Unable to create or inherit binding"的 GIN AssistedInject 问题

google-app-engine - 带有 Google Cloud Endpoints 和 Guice 的 Appengine

java - Guice @Inject 无法使用多个构造函数注入(inject)