Hi Jesse,
Well, basically I am trying to do the following. I would like to
inject a parent injector to a class so that it could create a child
injector from it.
It works if this class is created NOT via assisted factories, but
breaks otherwise.
I have the following declarations:
interface A {
}
class AImpl implements A {
String s;
Injector injector;
@Inject
public AImpl(@Assisted String s, Injector parent) {
this.s = s;
this.injector = parent.createChildInjector(new AbstractModule
() {
@Override
protected void configure() {
// bind local bindings here
}
@Provides
A provideA() {
return AImpl.this;
}
});
}
}
interface AFactory {
A create(String s);
}
Now I start it all like this:
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bind(AFactory.class).toProvider(FactoryProvider.newFactory
(AFactory.class, AImpl.class));
}
});
AFactory aFactory = injector.getInstance(AFactory.class);
A a = aFactory.create("test");
here I get the following error:
com.google.inject.ProvisionException: Guice provision errors:
1) A binding to guice.test2.A was already configured at
guice.test2.AFactory.create().
at guice.test2.AImpl$1.providea(AssistedTest.java:44)
at guice.test2.AImpl.<init>(AssistedTest.java:35)
while locating guice.test2.AImpl
while locating guice.test2.A
It certainly looks like a bug. In fact there are other errors if I say
remove @Provides method here and add another assisted factory binding
(which also has String parameter) into the child injector. In this
case it complains about duplicate binding for @Assisted String
(parameter to the factory). It does not make much sense to me.
Of course I can workaround this problem by not injecting the Injector
here and creating child injector in AImpl in a separate method, but
this kind of breaks the whole idea.
If you can shed some light on that it would be great!
BTW thanks for working on guice! :)
On Jun 9, 1:00 am, "[email protected]" <[email protected]> wrote:
> Hey Dmitry --
>
> Seems like you're really pushing the edges of parent injectors!
> AssistedInject is implemented using a child injector behind-the-
> scenes. This is required so that factory-created classes can
> participate in AOP.
>
> I'm curious about the duplicate binding errors - it was my goal that
> child injectors would be able to accomplish everything that their
> parent injectors could do. For example, asking that injector for a
> binding should just delegate to the parent injector. Could you shed
> some light on this?
>
> In any event, thanks for writing in with these issues. I'll do what I
> can to make things right.
>
> Cheers,
> Jesse
>
> On Jun 8, 3:55 pm, Dmitry Skavish <[email protected]> wrote:
>
> > If I have I class:
>
> > class A {
> > @Inject A(Injector injector) {}
>
> > }
>
> > then when created this instance will hold a reference to our original
> > injector.
>
> > If I have a class:
>
> > class A {
> > @Inject A(@Assisted String name, Injector injector) {}
>
> > }
>
> > and create this class using assisted factories like this:
>
> > interface AFactory {
> > A create(String name);
>
> > }
>
> > bind(AFactory.class).toProvider(FactoryProvider.newFactory
> > (AFactory.class, A.class));
>
> > then the injector which A received in its constructor is different
> > from our original injector and it seems it's not really a valid
> > injector because you can't do much with it, it throws exceptions
> > complaining about duplicate bindings all the time. I can provide all
> > those errors, but it seems to me that it all could be fixed by just
> > providing the correct (original) injector for factories created
> > objects.
>
> > Anybody has any insights into this? Am I doing something wrong?
> > Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---