2

I've been using Guice on project, and I'm trying to understand how the default injected providers work. In the "Injecting Providers" section of the manual (https://github.com/google/guice/wiki/InjectingProviders) there's the following simple example:

public class RealBillingService implements BillingService {
  private final Provider<CreditCardProcessor> processorProvider;
  private final Provider<TransactionLog> transactionLogProvider;

  @Inject
  public RealBillingService(Provider<CreditCardProcessor> processorProvider,
      Provider<TransactionLog> transactionLogProvider) {
    this.processorProvider = processorProvider;
    this.transactionLogProvider = transactionLogProvider;
  }

  public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
    CreditCardProcessor processor = processorProvider.get();
    TransactionLog transactionLog = transactionLogProvider.get();

    /* use the processor and transaction log here */
  }
}

Now, what I would like to know is what the default implementations of processorProvider.get() & transactionLogProvider.get() do exactly.

For example:

  • always create a new instance of CreditCardProcessor/TransactionLog
  • use an pool of objects
  • something else..

I'm asking this because I'm currently experincing some odd behaviour in my project that might be explained if the default provider uses some kind of caching strategy..

Thanks

0 Answers0