Why provider is in different scope by defining it in two similar ways ?

39 views
Skip to first unread message

Michael

unread,
Jan 5, 2013, 5:34:22 AM1/5/13
to google...@googlegroups.com
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).
 

Thomas Broyer

unread,
Jan 5, 2013, 8:31:33 AM1/5/13
to google...@googlegroups.com

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)".

Mikhail Mazursky

unread,
Jan 5, 2013, 7:14:48 AM1/5/13
to google...@googlegroups.com
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 <fuh...@gmail.com>:
> --
> 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 google...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-guice...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.

Michael

unread,
Jan 5, 2013, 10:01:35 PM1/5/13
to google...@googlegroups.com
Many thanks! Much more clear.
在 2013年1月5日星期六UTC+8下午9时31分33秒,Thomas Broyer写道:

Michael

unread,
Jan 5, 2013, 10:01:53 PM1/5/13
to google...@googlegroups.com
Many thanks! Much more clear.
在 2013年1月5日星期六UTC+8下午8时14分48秒,Mikhail Mazursky写道:
Reply all
Reply to author
Forward
0 new messages