You need to require the Map<String,String>, not the raw Map class.
requireBinding(Key.get(new TypeLiteral<Map<String,String>>(){}, MyMap))
On Monday, September 19, 2016 at 3:25:03 PM UTC+2, Steve Storck wrote:
>
> Some of my modules require a binding from another module. I want to set
> up a requireBinding in the dependent modules' configure() method, but it
> doesn't seem to be working for me. Here is an example of what I am doing:
>
> @BindingAnnotation
> @Target([FIELD, PARAMETER, METHOD])
> @Retention(RUNTIME)
> @interface MyMap {
> }
>
> class ModuleA extends AbstractModule {
> protected void configure() {
> }
>
> @Provides
> @Singleton
> @MyMap
> static def Map<String, String> myMap() {
> ImmutableMap.copyOf(
> [A: 'val1',
> B: 'val2']
> )
> }
> }
>
> class ModuleB extends AbstractModule {
> protected void configure() {
> requireBinding(Key.get(Map, MyMap))
> bind Foo
> }
> }
>
> class ModuleIT extends Specification {
> def 'Test module configuration'() {
> setup:
> def injector = Guice.createInjector new ModuleA(), new ModuleB()
>
> when:
> def result = injector.getInstance Foo
>
> then:
> result instanceof Foo
> result.someMap instanceof Map<String, String>
> }
> }
>
> This results in an error when createInjector is called:
> com.google.inject.CreationException: Unable to create injector, see the
> following errors:
> 1) No implementation for java.util.Map annotated with interface
> a.b.c.MyMap was bound.
>
> On the other hand, it works fine if I don't specify requreBinding.
> However, since this dependent module has no notion of the map that is
> bound, I want to ensure that the binding is present when the dependent
> module is created.
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/a3614d34-5997-424a-bd61-ecdc3b94bf9d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.