What is the difference between the @Singleton annotation on Dagger2 @Component annotated classes, and @Provides annotated methods in modules?
If I have one module in which all methods are annotated with a @Singleton annotation, and a component with the same annotation which includes that module, what is the purpose of this?
@Singleton
@Component(...)
public interface AppComponent {
// ...
}
And
@Provides @Singleton Context provideContext() { return context; }
Annotating the @Provides method (or the class with an @Inject constructor) tells Dagger to implement the actual scoping functionality whereas annotating the component (which is necessary) doesn't have any functionality, but tells Dagger "I allow this component to contain bindings of this scope". Note that you can still have unscoped bindings in a scoped component, but not the other way around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With