Many thanks! Much more clear.
在 2013年1月5日星期六UTC+8下午9时31分33秒,Thomas Broyer写道:
>
>
> On Saturday, January 5, 2013 11:34:22 AM UTC+1, Michael wrote:
>>
>> Hi All,
>>
>> I found a very interesting thing about the scope of the provider. Here is
>> the code of BaseModelProvider
>>
>> public class BaseModelProvider implements Provider<Model> {
>> private int num;
>> @Override
>> public Model get() {
>> System.out.println(num++);
>> return new Model();
>> }
>> --------------------------------------------------------------------
>> this.bind(Model.class).annotatedWith(Names.named("index")).toProvider(new
>> BaseModelProvider());
>>
>> This way gives : 1,2,3,4,5,6.....(in singleton scope)
>> ---------------
>>
>> this.bind(Model.class).annotatedWith(Names.named("index")).toProvider(BaseModelProvider.class);
>> This way gives : 0,0,0,0,0.....(in request scope)
>>
>>
>> I don't understand the different about this two definitions. So anybody
>> knows how it works ? many thanks!
>>
>
> In the first case, you provide a Provider instance, so only this instance
> will be used, making it a de-facto singleton.
> In the second case, Guice will create the provider just like any other
> object. If you want it to be a singleton, then annotate it with @Singleton
> or bind it as a singleton with
> "bind(BaseModelProvider.class).in(Singleton.class)".
>
> This is similar to "bind(Foo.class).toInstance(new MyFoo())" vs.
> "bind(Foo.class).to(MyFoo.class)".
>
--
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/-/2o-zWMQs2dwJ.
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.