guice multibinder with Providers

前端 未结 2 859
谎友^
谎友^ 2021-01-07 10:34

I am trying to be able to have this in my code

@Inject
private Map> providers;

I was trying but thi

相关标签:
2条回答
  • 2021-01-07 11:23

    Though Guice seems to be very good about using JSR-330 annotations interchangeably, it seems that Multibindings hides the Provider type within a Map and therefore may be expecting to inject a java.util.Map<java.lang.String, com.google.inject.Provider<...>> instead. I haven't been able to reproduce your problem, but try that and see if it helps.

    Side note: If you want to avoid changing it in code everywhere, you can hackishly bind a provider of Map<String, javax.inject.Provider<Foo>> to something that takes in the multibinder-created Map<String, com.google.inject.Provider<Foo>>. If I'm right and this is the problem, you can fix it one place rather than bouncing between javax.inject and com.google.inject everywhere.

    0 讨论(0)
  • 2021-01-07 11:32

    If you want Map<String, Provider<Processor>> at the injection point, the way to bind it is:

    MapBinder<String, Processor> mapbinder = MapBinder.newMapBinder(binder, String.class, Processor.class); mapbinder.addBinding("splineV1Beta").to(SplineProcessor.class); mapbinder.addBinding("invertV1Beta").to(InvertProcessor.class);

    You would use toProvider() if you are supplying a custom provider, which you are not, i.e. you are merely trying to use the implicit underlying provider.

    0 讨论(0)
提交回复
热议问题