Hi Dan,

Thanks for repying, I've been meaning to follow up on this thread.

I had originally done some trickery to bind DoSomething to the child  
injector, and after I made my first post it became clear to me that  
that might not have worked the way I expected.

I changed my module to be more specific by simply saying  
"bind(DoSomething.class);" in the child (no to() statement or  
anything), and that solved my problem.  References to the Injector in  
objects instantiated by the child subsequently referred to the child  
rather than parent injector.  It may or may not solve the problem for  
you.

I believe the solution that Dhanji is proposing probably would have  
worked as well.  You could  
bind 
(Injector 
.class 
).annotatedWith 
(Names.named("child").toProvider(ChildInjectorProvider.class), then  
create a ChildInjectorProvider class whose get method returns the  
child injector.

I imagine that you'd have to annotate the binding like I did above  
(since Injector is automatically bound) but I don't know for sure as I  
haven't tried this directly.

Mike




On Aug 20, 2009, at 5:22 AM, Dan Godfrey wrote:

>
> Could you elaborate further, we've hit exactly the same issue and
> can't get around it.
>
> Thanks,
> Dan.
>
>
> On Aug 15, 10:55 am, "Dhanji R. Prasanna" <[email protected]> wrote:
>> The easy answer is to simply bind the child injector using a  
>> provider.
>> Dhanji.
>>
>> On Sat, Aug 15, 2009 at 10:11 AM, Michael Burton <[email protected]>  
>> wrote:
>>
>>> I have a class that requires an Injector to do some work (it needs  
>>> to
>>> construct some objects on the fly and then injectMembers for them).
>>
>>> class DoSomething {
>>>    @Inject protected Injector injector;
>>
>>>    public void doIt() {
>>>        ...
>>>        injector.injectMembers(stuff);
>>>        ...
>>>    }
>>> }
>>
>>> In my application, I have two levels of injector.  I have a master
>>> injector, created using Guice.createInjector(), and I have a few  
>>> child
>>> injectors, created by calling master.createChildInjector().  I've
>>> verified that DoSomething was created using a binding in one of the
>>> child injectors.
>>
>>> The weird thing is that if I inspect DoSomething.injector, I find  
>>> that
>>> it is the MASTER injector, not the CHILD injector.  I would have
>>> expected the Injector to be the child injector since it was the  
>>> child
>>> that instantiated DoSomething.
>>
>>> This seems to be causing me problems like the following:
>>
>>> E/AndroidRuntime( 1515): com.google.inject.ConfigurationException:
>>> Guice configuration errors:
>>> E/AndroidRuntime( 1515):
>>> E/AndroidRuntime( 1515): 1) A binding to
>>> com.google.inject.Provider<android.app.Activity> already exists on a
>>> child injector.
>>> E/AndroidRuntime( 1515):   while locating
>>> com.google.inject.Provider<android.app.Activity>
>>> E/AndroidRuntime( 1515):     for field at
>>> com.XXX.http.BaseHttpGet.activityProvider(Unknown Source)
>>> E/AndroidRuntime( 1515):   while locating com.XXX.model.ImageUtils
>>> $GetImage
>>> E/AndroidRuntime( 1515):
>>> E/AndroidRuntime( 1515): 2) A binding to com.XXX.http.Http already
>>> exists on a child injector.
>>> E/AndroidRuntime( 1515):   while locating com.XXX.http.Http
>>> E/AndroidRuntime( 1515):     for field at  
>>> com.XXX.http.BaseHttpGet.http
>>> (Unknown Source)
>>> E/AndroidRuntime( 1515):   while locating com.XXX.model.ImageUtils
>>> $GetImage
>>> E/AndroidRuntime( 1515):
>>
>>> My interpretation of that error is that when I call
>>> injector.injectMembers(), the master injector attempts to generate a
>>> few bindings that it can't find, but errors out because those  
>>> bindings
>>> already exist in the child injector.
>>
>>> Is there a way I can tell Guice to inject the "correct" Injector
>>> instead of always the root injector?  Or alternately, is there a  
>>> way I
>>> can get the correct injector manually somehow?
>>
>>> Cheers,
>>> Mike
> >


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to