On 16 May 2010 06:35, yurique <[email protected]> wrote:

> > does the class "this.persister" refers to have any methods
> or fields annotated with @Inject?
>
> Yes, this object is built with guice within another module of an
> application and has fields annotated with @Inject.
>
> > Guice is probably trying to Inject them.
>
> Thats what it seems to me, too. But I can't find a reason for it to do
> so - the object is already built and bound using "toInstance".
>
> Does this mean, that one cannot bind objects built by Guice (with
> different Module and bindings)? This doesn't seem right to me.
>

FYI, this behavior is mentioned on the Guice wiki / user-guide under the
Auto Injections heading:

   http://code.google.com/p/google-guice/wiki/Injections

   "Guice automatically injects all of the following:

   - instances passed to toInstance() in a bind statement
   - provider instances passed to toProvider() in a bind statement

   The objects will be injected while the injector itself is being created."

as Sam mentioned you can use Provider.of() or @Provides methods to avoid
this auto-injection:

   http://code.google.com/p/google-guice/wiki/ProvidesMethods

or use a service-locator:
http://martinfowler.com/articles/injection.html#UsingAServiceLocator

HTH

I've  just created a simple test project.
>
> I have the following classes within it:
>
> ITool - a simple interface
> SpecificTool - some implementation of that interface
>
> IService - another interface
> SpecificService - a service that uses an ITool interface (@Inject
> private ITool tool)
>
> Client - a simple class, that uses an IService (@Inject private
> IService service)
>
>
> Here's what I do in the test:
> * I create one AbstractModule - with bindings for ITest and for
> IService
> * create an injector using that module and create an instance of
> IService (actually an instance of SpecificService)
>
> Then I create another AbstractModule (and injector) in which I bind
> the IService.class to the created object using
> bind(...).toInstance(...). This module doesn't "know" about ITool.
> Now using this new injector I try to create an instance of Client,
> which has an injected IService field. And Guice fails complaining that
> ITool is not bound.
>
> But at this moment Guice has everything needed to successfully build
> Client - because IService is already bound to an *existing* instance.
>
>
>
>
>
> --
> 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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>


-- 
Cheers, Stuart

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