Search code examples
dagger-2

Difference between @Singleton & static @Provides in dagger2


May I know the difference between @Singleton VS static Provides in dagger2?

@Provides static User currentUser(AuthManager authManager) {
    return authManager.currentUser();
}

@Provides @Singleton User currentUser(AuthManager authManager) {
    return authManager.currentUser();
}

Solution

  • These are very different attributes, and you can have one or the other independently. All of these are valid:

    @Provides User currentUser(...) {}
    @Provides static User currentUser(...) {}
    @Provides @Singleton User currentUser(...) {}
    @Provides @Singleton static User currentUser(...) {}
    

    To set the stage, a @Provides User method says "for this Component or its dependencies, call this @Provides method every time you need a User". Typically the method will return a new instance every time, and Dagger won't save or cache the instance.

    @Singleton is an example of a scope, which is a fancy way to say lifecycle policy or policy for how often to create a new instance. @Provides @Singleton User says "for this Component or dependencies, just call this @Provides method once, and save the result". @Singleton happens to be a built-in common case, but you could also imagine creating a @UserScope (always return the same instance for this User), or in Android a @FragmentScope or @ActivityScope.

    For your specific case, you probably don't want @Singleton, because it would instruct your component to save or cache the value from AuthManager. If the User value may change across your application's lifetime, the Component wouldn't reflect that. (In that case you would also want to make sure to inject Provider<User>, which would update, rather than User which would not.)

    Leaving scopes behind for a moment, static behaves exactly the way you would expect it to in Java: If a method doesn't require any instance state, you can make it static, and your virtual machine can call it without preparing any instance state. In your generated Component implementation, Dagger will automatically call static methods statically, and instance methods on the Module instance you pass into your Component; in Android this results in a sizable performance increase. Because you don't use any instance state in your currentUser method, it can easily be made static.

    Further reading: