Trying to understand dependency injection using Guice. In which cases do we use @Provides
and which ones do we use the toProvider
binding in the module?
Asked
Active
Viewed 1,024 times
1
-
Use whatever makes your code prettier. Also it's `@Provides` not `@Provider`. – Tavian Barnes May 10 '15 at 21:54
1 Answers
2
When you use @Provides
, you write one method in your module. When you use toProvider
, you actually create an entire class, which has all the complexity of making a class, as opposed to a single method.
Ultimately, both work, and both allow you to pass in injected dependencies. Just choose the one that is most appropriate for the amount of complexity you need in your specific use case.
Remember that @Provides
methods cannot throw Exceptions; so exception handling could be one reason to offload the complexity to a full class.

durron597
- 31,968
- 17
- 99
- 158