I have investigated this through experimentation. You can control the
scope of the Provider and/or the object being provided separately.

The following code ensures that a single Provider is created

        bind(IConsumer.class).toProvider(ConsumerProvider.class);

        bind(ConsumerProvider.class).asEagerSingleton();

While the following code ensures that BOTH the provider and the object
provided are singletons:

 
bind(IConsumer.class).toProvider(ConsumerProvider.class).asEagerSingleton();

While the following code ensures every injection of a provider and
every such object provided are distinct objects:

        bind(IConsumer.class).toProvider(ConsumerProvider.class);

On Jan 9, 6:05 am, Sam Berlin <[email protected]> wrote:
> The original poster was asking about the Provider itself, not the scope of
> what it provides.
>
> sam
> On Jan 9, 2012 4:45 AM, "unguiculus" <[email protected]> wrote:
>
>
>
>
>
>
>
> > Sam,
>
> > This is not quite correct. @Provides methods create unscoped instances
> > unless they are annotated with a scope annotation (e. g. @Singleton).
> > I think the documentation on this could be improved. It should
> > especially be mentioned that scoping is possible.
>
> >http://code.google.com/p/google-guice/wiki/ProvidesMethods
>
> > Reinhard
>
> > > I think the catch is that toProvider(SomeProvider.class) is an unscoped
> > > provider binding, so each injection of Provider<T> will create a new
> > > instance of the Provider (I think), so there's no shared state since each
> > > instance is different.  @Provides is analogous to
> > > toProvider(providerInstance), which in turn is analogous to
> > > bind(..).toInstance(..), all of which act as singletons and need to be
> > > thread-safe.
>
> > > sam
>
> > --
> > 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.

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