Many thanks! Much more clear.
在 2013年1月5日星期六UTC+8下午8时14分48秒,Mikhail Mazursky写道:
>
> The first way you implicitly bind to an instance (so provider is 
> singleton itself - guice will never instantiate it): 
> bind(BaseModelProvider.class).toInstance(new BaseModelProvider()); 
> bind(Model.class).annotatedWith(Names.named("index")).toProvider(BaseModelProvider.class);
>  
>
>
> The second way you implicitly bind in prototype scope (no scope): 
> bind(BaseModelProvider.class); 
> bind(Model.class).annotatedWith(Names.named("index")).toProvider(BaseModelProvider.class);
>  
>
>
> If you want your providers to be in some scope then you should let 
> Guice create them for you and either annotate them with scoping 
> annotation (e.g. with @Singleton) or bind them in scope (like 
> bind(BaseModelProvider.class).in(Singleton.class);). 
>
> 2013/1/5 Michael <[email protected] <javascript:>>: 
> > 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! 
> > 
> > BTW: I just want bind many providers dynamically within a loop, So I am 
> > using the .toProvider(Prvider<? extends Model> instance). 
> > 
> > 
> > -- 
> > 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/-/ZsZa_iHVMdAJ. 
> > To post to this group, send email to 
> > [email protected]<javascript:>. 
>
> > To unsubscribe from this group, send email to 
> > [email protected] <javascript:>. 
> > 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 view this discussion on the web visit 
https://groups.google.com/d/msg/google-guice/-/BAGyLCyyoogJ.
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