First step: I create container object by help a Guice which has a module
with container implementation class.
Second step: inside the container I create child injector in which I want
replace the container object implementation binding to instance of this
container.
Third step: I create child object via child injector so from now I can
inject the parent object to child object constructor.
In other words I want to replace container implementation binding in child
injector, for example:
bind(Container.class).to(ContainerImpl.class)
to concrete instance of created container (inside the container object):
bind(Container.class).toInstance(this)
and use this new binding for creating child objects.
I have small workaround for this, but it now works for Provider bindings:
public static <T, I extends T> Injector
createInjectorWithBindReplacement(final Injector sourceInjector,
final
Class<T> clazz, final I instance) {
return Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(clazz).toInstance(instance);
Map<Key<?>, Binding<?>> srcInjectorBindings =
sourceInjector.getAllBindings();
for (Map.Entry<Key<?>, Binding<?>> srcKeyBindingEntry :
srcInjectorBindings.entrySet()) {
Key<?> srcKey = srcKeyBindingEntry.getKey();
if (srcKey.equals(Key.get(Injector.class))
||
srcKey.equals(Key.get(java.util.logging.Logger.class))) {
continue;
}
if (srcKey.equals(Key.get(clazz))) {
continue;
}
System.out.println(srcKeyBindingEntry); // todo remove
srcKeyBindingEntry.getValue().applyTo(binder());
}
}
});
}
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/lR1svopX9aMJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.