问题描述
对于Dagger2的 @Bind注释,我不确定./a>
I am not certain on the purpose for Dagger2's @Bind annotation.
根据我在网上阅读的内容,我仍然不清楚,但这是一个示例:
From what i have read online im still not clear but here is an example:
@Module
public abstract class HomeModule {
@Binds
public abstract HomePresenter bindHomePresenter(HomePresenterImp
homePresenterImp);
}
和类定义如下:
public interface HomePresenter {
Observable<List<User>> loadUsers();
}
public class HomePresenterImp implements HomePresenter {
public HomePresenterImp(){
}
@Override
public Observable<List<User>> loadUsers(){
//Return user list observable
}
}
如果我只能使用提供注释,为什么我需要使用@Binds:
why would i need to use @Binds if i can just use provides annotation as follows:
@Provides
public HomePresenter provideHomePresenter() {
return new HomePresenterImp();
}
@Binds而不是@Provides的用例是什么?如果我使用@Binds,我是否仍需要在我的appcomponent中声明它(当我使用@Binds时,它是一个抽象类)?
what is the usecase for @Binds instead of @Provides ? if i use @Binds do i still need to declare it in my appcomponent (its an abstract class when i use @Binds)?
推荐答案
@Binds可以完全等同于@Provides注释的方法,如下所示:
@Binds can be perfectly equivalent to a @Provides-annotated method like this:
@Provides
public HomePresenter provideHomePresenter() {
return new HomePresenterImp();
}
...尽管您可能更喜欢使用HomePresenterImp作为方法参数的变体,该变体允许Dagger实例化HomePresenterImp(假设它具有@Inject构造函数),包括传递所需的任何依赖项.您还可以创建static
,因此Dagger无需实例化Module实例即可调用它.
...though you'd probably prefer a variant that takes HomePresenterImp as a method parameter, which lets Dagger instantiate HomePresenterImp (assuming it has an @Inject constructor) including passing any dependencies it needs. You can also make this static
, so Dagger doesn't need to instantiate your Module instance to call it.
@Provides
public static HomePresenter provideHomePresenter(HomePresenterImp presenter) {
return presenter;
}
那为什么要选择@Binds
呢? Dagger对此有常见问题解答,但归结为以下原因:
So why would you choose @Binds
instead? Dagger has a FAQ about it, but it boils down do these reasons:
- @Binds(略)更紧凑:您可以跳过实现.
- @Binds在接口和抽象类中工作,而@BindsOptionalOf和@ContributesAndroidInjector之类的Dagger功能则严格要求这些接口和抽象类.
- @Binds帮助您的代码保持高效. @Provides方法可以是实例方法,需要Dagger实例化您的Module才能调用它们.使您的@Provides方法
static
也可以完成此操作,但是如果您忘记了static
,您的@Provides方法仍然可以编译. @Binds方法不会. - @Binds防止Dagger进行代码生成并为对象保留单独的Factory/Provider,因为Java不允许Dagger访问其实现是多么简单.在您的情况下,Dagger可以将
Provider<HomePresenterImp>
强制转换为Provider<HomePresenter>
,并且只保留一个,而不是为HomePresenter保留一个,而只为HomePresenterImp调用一个.
- @Binds is (slightly) more compact: You can skip the implementation.
- @Binds works in interfaces and abstract classes, which are strictly required for Dagger features like @BindsOptionalOf and @ContributesAndroidInjector.
- @Binds helps your code stay efficient. @Provides methods can be instance methods, which require Dagger to instantiate your Module in order to call them. Making your @Provides method
static
will also accomplish this, but your @Provides method will still compile if you forget thestatic
. @Binds methods will not. - @Binds prevents Dagger from having to codegen and keep a separate Factory/Provider for the object, since Java doesn't give Dagger access to know that the implementation is as simple as it is. In your case, Dagger can cast the
Provider<HomePresenterImp>
to aProvider<HomePresenter>
and only keep one, rather than keeping one for HomePresenter that does nothing but call the one for HomePresenterImp.
因此,整个事情都可以很好地表示为:
Thus, the entire thing would be well-represented as:
@Binds abstract HomePresenter bindHomePresenter(HomePresenterImp presenter);
这篇关于Dagger2中@Binds与@Provides批注的用例是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!