How to use a Guice injector?

I am learning Guice and I do not understand how to use the Injector instance. Better to create an Injector instance once in a bootstrap application and make it singleton publicly available?

And is it true that we should always use Injector#getInstance(SomeClass.class) to get classes where we put Guice @Inject annotations?

+7
source share
1 answer

You should not pass the injector as a global singleton. Have you looked: https://github.com/google/guice/wiki/GettingStarted ? Note that the RealBillingService does not use an injector to receive instances of CreditCardProcessor and TransactionLog . Instead, Guice handles all this for you when creating the instance.

If you are in a situation where you need Guice to create multiple objects of the same type, consider using a Provider and entering a provider.

+7
source

All Articles