Thanks alot, that all makes total sense. I just was thinking, since Guice is the one calling the provider method, it could know the instance I'm creating is supposed to be a singleton, and enforce that somewhere along the line. I guess any "new" not done by Guice sides steps that ability.
On Friday, April 12, 2013 2:11:58 PM UTC-7, Jeff wrote: > > If your @Provider does not have @Singleton, Guice will call the provider > for every instance it is asked for and run whatever code is defined. > > Doing a 'new' (or anything else for that matter) inside the provider is > outside Guice's scope, unless you are interacting with the @Injector > directly. Guice must manage the object lifecycle. > > What I don't know is if Guice looks for and honors the @Singleton > annotation on the class itself or whether it must be defined as singleton > via the bindings. > > > On Fri, Apr 12, 2013 at 2:34 PM, Alper Akture > <[email protected]<javascript:> > > wrote: > >> I have an @Provides method in a module that creates an object for me, and >> the class of that object has @Singleton on it. I found that merely having >> the @Singleton on the class does not enforce a single instance being >> created. After adding @Singleton to the @Provider method, I do get a single >> instance. Even though I am doing a "new" on the object in my provider >> method, couldn't Guice see that the @Provider method is creating it, and do >> the same thing it does (keep a single instance handy and give it out when >> requested) when having the @Singleton on the provider method? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-guice" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> To post to this group, send email to [email protected]<javascript:> >> . >> Visit this group at http://groups.google.com/group/google-guice?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > > > -- > Jeff Vincent > See my LinkedIn profile at: > http://www.linkedin.com/in/rjeffreyvincent > I ♥ DropBox <http://db.tt/9O6LfBX> !! > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
