How to get guice to provide a unique instance with a provider?

31 views
Skip to first unread message

Maatary Okouya

unread,
Aug 24, 2014, 10:38:06 AM8/24/14
to google...@googlegroups.com
Hi, 


I have an object A that does some important loading. I use a provider for it. I have object B that needs A for its initialization. However in an object C I want to pass A, and B, properly prepared. 

Would it be possible to get a provider to return the same instance all the time, or the guice module to keep that instance provided and stop right there?


Many thanks,

-M-

Maatary Okouya

unread,
Aug 24, 2014, 10:57:18 AM8/24/14
to google...@googlegroups.com
I have the following on the guice website with few explanation:

  @Provides @Singleton
  TransactionLog provideTransactionLog() {
    ...
  }

Is that the solution. Does the singleton apply to the provider or the provided. If it applies to the provider then, the solution would be to retain one instance and return it each time. But that would not work with a provide method.
Any help ?

Chad Urso McDaniel

unread,
Aug 26, 2014, 5:02:05 PM8/26/14
to google...@googlegroups.com

On Sun, Aug 24, 2014 at 7:57 AM, Maatary Okouya <maatar...@gmail.com> wrote:
I have the following on the guice website with few explanation:

  @Provides @Singleton
  TransactionLog provideTransactionLog() {
    ...
  }

Is that the solution. Does the singleton apply to the provider or the provided. If it applies to the provider then, the solution would be to retain one instance and return it each time. But that would not work with a provide method.
Any help ?

Yes, @Singleton does what you want.
Reply all
Reply to author
Forward
0 new messages