@Provides singleton behavior

2,900 views
Skip to first unread message

Alper Akture

unread,
Apr 12, 2013, 4:34:16 PM4/12/13
to google...@googlegroups.com
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?

Jeff

unread,
Apr 12, 2013, 5:11:58 PM4/12/13
to google...@googlegroups.com
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 <alper....@gmail.com> 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 google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
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 !! 

Alper Akture

unread,
Apr 12, 2013, 6:43:15 PM4/12/13
to google...@googlegroups.com
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.

Christian Gruber

unread,
Apr 12, 2013, 6:56:10 PM4/12/13
to google...@googlegroups.com
It sidesteps Guice unless it is within a @Provides method, or a Provider
instance that is bound, in which case guice still manages the lifecycle
but delegates to the Provides method to do the actual creation itself.

Christian

On 12 Apr 2013, at 17:43, Alper Akture wrote:

> 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
>> <alper....@gmail.com<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 google-guice...@googlegroups.com <javascript:>.
>>> To post to this group, send email to
>>> google...@googlegroups.com<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 google-guice...@googlegroups.com.
> To post to this group, send email to google...@googlegroups.com.
> Visit this group at http://groups.google.com/group/google-guice?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.


Christian Gruber :: Google, Inc. :: Java Core Libraries :: Dependency
Injection
email: cgr...@google.com :::: mobile: +1 (646) 807-9839
Reply all
Reply to author
Forward
0 new messages