Ok, I gave the child injector approach another shot. I removed all
factories and @Assisteds so that the dependency is simple A->B->C->D.
Here's what I came up with:
class DModule extends AbstractModule {
D d;
DModule( D d ) {
this.d = d;
}
protected void configure() {
bind( D.class ).toInstance( d );
}
}
Then in my main() method, I first tried:
Injector i = Guice.createInjector( new MyModule() );
Injector c1 = i.createChildInjector( new DModule( new D() ) );
A a = c1.getInstance( A.class );
a.executeA();
Injector c2 = i.createChildInjector( new DModule( new D() ) );
a = c2.getInstance( A.class );
a.executeA();
This didn't work. Guice ignored the "new D()" I bound in the DModule
for each child injector and just injected . So I tried using
Modules.override() instead:
Injector i1 = Guice.createInjector( Modules.override( new
MyModule() ).with( new DModule( new D() ) ) );
A a1 = i1.getInstance( A.class );
a1.executeA();
Injector i2 = Guice.createInjector( Modules.override( new
MyModule() ).with( new DModule( new D() ) ) );
A a2 = i2.getInstance( A.class );
a2.executeA();
This did work. Obviously the problem with both of these is that any
state I wanted to preserve in my first instance of class A gets lost,
because I have to create two different instances.
I feel like I'm walking down the right path with this, but can't quite
get over the hump.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
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.