EDIT 2018-02-08: Sample project demonstrating how to do this at https://github.com/ravn/dagger2-named-string-inject-example - Note: the whole source is in a single file!
I am looking at whether dagger can replace guice for us (as our deployment Java platform is slow).
I construct a map of configuration strings at runtime, which I would like to have dagger inject as needed.
E.g. If I have
java.util.Map<String, String> map = new java.util.TreeMap<String, String>();
map.put("key", "value");
and
@Inject
Thermosiphon(Heater heater, @Named("key") String value) {
this.heater = heater;
System.out.println("value =" + value);
}
I would like to have "value" injected in value.
The examples in the source code does not have any @Named usages. Just trying gives the following exception:
Exception in thread "main" java.lang.IllegalStateException: Errors creating object graph:
No binding for @javax.inject.Named(value=key)/java.lang.String required by class bar.Thermosiphon
at dagger.internal.ThrowingErrorHandler.handleErrors(ThrowingErrorHandler.java:34)
at dagger.internal.Linker.linkRequested(Linker.java:146)
at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:288)
at dagger.ObjectGraph$DaggerObjectGraph.get(ObjectGraph.java:249)
at app.CoffeeApp.main(CoffeeApp.java:20)
How should I approach this?