Search code examples
javaguiceinject

Expose Map from guice private Module


I am trying to expose a map from private module similar to Expose Map in private guice module but getting an error

Could not expose() java.util.Map annotated with @com.google.inject.name.Named(value=myMap), it must be explicitly bound.

I have written this code:

expose(Map.class).annotatedWith(Names.named(myMap));

@Named("myMap")
@Provides
@Singleton
public Map<String, String> myMap() {
    // myMap
    return ImmutableMap.<String, String>builder()
            .build();
}

Also in another class where I am injecting it, it gives:

No implementation for java.util.Map<java.lang.String, java.lang.String> was bound.

Please check. thanks


Solution

  • To this Guice offers multibinders and, in particular, for your case, MapBinder which in its Javadoc provides a nice code example.