Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between scope in modules and components

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; }
like image 206
Near1999 Avatar asked Jan 18 '26 18:01

Near1999


1 Answers

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.

like image 87
rdshapiro Avatar answered Jan 21 '26 06:01

rdshapiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!