I know this chain is old, but I haven't seen another (more recent) posting that talks about this. I'm still confused about this. Basically, I'd like to inject the child injector as well. I have a case where I cannot use the FactoryModuleBuilder and would like to pass the child injector. I run into the same problem where the injector is actually the root when I was expecting the child.
I will provide some sample code below, but if someone could give a full example of the provider solution above, I would be much obliged. When I try to make the provider solution work, I get an error from Guice saying I basically have no right to try to inject the Injector class, even using the "Names.named()" approach.
// The interface of what I want to inject
public interface IDriver
{
}
// Implementation of what I want to inject
public class Driver implements IDriver
{
private final Injector injector;
@Inject
public Driver(Injector injector)
{
this.injector = injector;
System.out.println("Driver injector: '" + this.injector.hashCode() + "'.");
}
}
// Parent module
public class ParentModule extends AbstractModule
{
@Override
protected void configure()
{
}
}
// Child module
public class ChildModule extends AbstractModule
{
@Override
protected void configure()
{
bind(IDriver.class).to(Driver.class).in(Singleton.class);
}
}
//Entry point/program
public class RunTestChildInjector
{
public static void main(String[] args)
{
Injector parent = Guice.createInjector(new ParentModule());
System.out.println("Parent injector: '" + parent.hashCode() + "'.");
Injector child = parent.createChildInjector(new ChildModule());
System.out.println("Child injector: '" + child.hashCode() + "'.");
child.getInstance(IDriver.class);
}
}
// Results:
Parent injector: '1592418026'.
Child injector: '1582325328'.
Driver injector: '1592418026'.
// Expected:
Child injector = Driver injector
Why do I want the injector to be the child?
- Basically because I have a situation where I want multiple instances of driver to exist, and have all other objects created by the child injector (not shown) to be associated with that driver
- The objects would also interact with "global" objects created by the parent module
- Of course, most are built automatically by Guice, but there are some where I want/need to use the Injector
So...
- To me it made intuitive sense that the injector used for the driver should have been the child injector, but I get a similar error as noted in the first post (Unable to create binding for ...It was already configured on one or more child injectors or private modules) when I try to take the injector in driver and get an instance of another object defined in the child module (not shown)
- When I checked the javadoc for createChildInjector I read "No key may be bound by both an injector and one of its ancestors. This includes just-in-time bindings. The lone exception is the key forInjector.class, which is bound by each injector to itself." This tells me that the injector would have the updated child key as the only exception, so I thought what I had above should have worked.
Basically, if this is as-designed, I'm looking for a full example of how to get and use the child injector inside the Driver object.
Any help is greatly appreciated.--
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/-/it5I-HuBP-wJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Hi Stuart,
Thank you! That makes sense. I added the bind(<implementing class>) below each bind in the child module and sure enough it worked.
I will look into adding the explicit bindings check as well, as I'd rather fail fast than get surprised later.
I guess what really threw me off was when I was debugging (as shown above), I was getting the parent injector object in Driver when I was expecting the child injector. I still don't quite get why, but at this point, I'm just happy it works.
Thanks again!