Junior — Middle
Mis on annotatsiooni Provides eesmärk Daggeris ja kuidas seda kasutatakse sõltuvuste pakkumiseks?
sobes.tech AI
Vastus AI-lt
The @Provides annotation in Dagger is used within a module to indicate a method that creates and provides a dependency. Such a method returns an object that will be injected into other components.
Example of usage:
@Module
class NetworkModule {
@Provides
OkHttpClient provideOkHttpClient() {
return new OkHttpClient.Builder().build();
}
}
Here, the provideOkHttpClient method creates and returns an instance of OkHttpClient. Dagger uses this method to supply the object when it needs to be injected into other classes. This allows centralized management of dependency creation and configuration.