>
> bind(TransactionLog.class).**toProvider(**TransactionLogProvider.class);

In this case, where @Singleton is added on top of FileTransactionLog, only
FileTransactionLog is a singleton, not TransactionLog. Those are two
different type.

 bind(TransactionLog.class).**toProvider(**TransactionLogProvider.class).**
> in(Singleton.class);

This binds TransactionLog as a singleton, thus will always return the same
instance when returning TransactionLog.

If you were to inject FileTransactionLog directly in your first example,
you would get the singleton you were expecting.


On Mon, Mar 18, 2013 at 10:28 PM, <google-gu...@googlecode.com> wrote:

> Comment by fcin...@gmail.com:
>
> I'm a bit confused about the behaviour of using Provider and @Singleton
> simultaneously.
>
> I tried to use a TransactionLogProvider to supply an instance of
> FileTransactionLog which is annotated with @Singleton. But multiple
> instances are still instantiated.
>
> Please refer to the snippets below. Two instances of FileTransactionLog
> would be created, which means @Singleton doesn't work for this scenario.
>
> BillingModule.java
> {{{
>   protected void configure() {
>     bind(TransactionLog.class).**toProvider(**
> TransactionLogProvider.class);
>     //singleton only works if it's explicitly declared as following
>     //bind(TransactionLog.class).**toProvider(**
> TransactionLogProvider.class).**in(Singleton.class);
>   }
>
> }}}
>
> TransactionLogProvider.java
> {{{
>   public TransactionLog get() {
>     return new FileTransactionLog();
>   }
> }}}
>
> FileTransactionLog with @Singleton
> {{{
> @Singleton
> public class FileTransactionLog implements TransactionLog {
>   public FileTransactionLog() {
>     System.out.println("new FileTransactionLog instance");
>   }
> }
> }}}
>
> Bootstrap
> {{{
>   public static void main(String[] args) {
>     Injector injector = Guice.createInjector(new BillingModule());
>
>     injector.getInstance(**RealBillingService.class);
>     injector.getInstance(**RealBillingService.class);
>
>   }
> }}}
>
> For more information:
> http://code.google.com/p/**google-guice/wiki/Scopes<http://code.google.com/p/google-guice/wiki/Scopes>
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to 
> google-guice-dev+unsubscribe@**googlegroups.com<google-guice-dev%2bunsubscr...@googlegroups.com>
> .
>
> To post to this group, send email to 
> google-guice-dev@googlegroups.**com<google-guice-dev@googlegroups.com>
> .
> Visit this group at http://groups.google.com/**
> group/google-guice-dev?hl=en<http://groups.google.com/group/google-guice-dev?hl=en>
> .
> For more options, visit 
> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
> .
>
>
>


-- 
Christian Goudreau
www.arcbees.com

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice-dev+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice-dev@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to