有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GUI要求绑定到TypeLiteral抛出错误

我有以下模块,需要重新绑定:

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中出现异常。谷歌。注射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的noob,因此希望能提供一些指针


共 (1) 个答案

  1. # 1 楼答案

    您需要以某种方式提供地图,否则guice不知道从哪里获取地图。这样的办法应该行得通

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