I could see maybe looking at the get() method for an annotation, but that becomes complicated with subclassing, etc. Still, maybe worth it. sam
On Wed, Jul 18, 2012 at 6:08 PM, Leigh Klotz, Jr. <[email protected]>wrote: > One thing I've wanted for a long time in Guice is to specify the scope of > the provided value with an annotation. > > You can easily mark the scope of the Provider itself being with an > annotation: > > @Singleton public class FooProvider implements Provider<Foo> { ... } > > And in a Provider method, the scope annotation applies to the resulting > object: > @Provides @Singleton Foo getFoo() { ... } > > You can do it in the module, with bind bind as described in Derrick's > message below: > bind(Foo.class).inScope(Scopes.SINGLETON); > bind(Foo.class).toProvider(FooProvider.class); > > But you can't mark it in annotations, at least as far as I can tell, > unless you use an @Provides method. > > It makes me want something like this: > @Provides(@Singleton) public class FooProvider implements Provider<Foo> > { ... } > > Leigh. > > > On Tuesday, January 10, 2012 2:16:49 PM UTC-8, Derrick wrote: >> >> 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<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 >> > > google-guice+unsubscribe@**googlegroups.com<google-guice%[email protected]>. >> >> > > For more options, visit this group at >> > >http://groups.google.com/**group/google-guice?hl=en<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 view this discussion on the web visit > https://groups.google.com/d/msg/google-guice/-/g6po9YMzbvEJ. > > 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.
