Dagger2:如何在同一模块中使用@Provides和@Binds [英] Dagger2 : How to use @Provides and @Binds in same module
问题描述
我正在使用新的Dagger2(2.11版),并且正在使用诸如AndroidInjector
和ContributesAndroidInjector
之类的新功能.我有一个活动子组件
I'm using the new Dagger2 (ver 2.11) and I'm using the new features like AndroidInjector
, and ContributesAndroidInjector
. I have an activity subcomponent,
@Module
abstract class ActivityBuilderModule {
@ContributesAndroidInjector(modules =
{UserListModule.class, MainFragmentModule.class})
@ActivityScope
abstract MainActivity bindsMainActivity();
}
@Module
public abstract class MainFragmentModule {
@ContributesAndroidInjector
@FragmentScope
@FragmentKey(UserListFragment.class)
abstract UserListFragment bindsUserListFragment();
}
UserListModule
提供了片段的依赖关系.我只想绑定实例并返回某些依赖项,例如
And the UserListModule
provides dependencies for the fragment. Some of the dependencies I just want to bind the instances , and return , like
@Binds
@ActivityScope
abstract UserListView mUserListView(UserListFragment userListFragment);
而不是简单地返回依赖项,例如
Instead of simply just return the dependency , like
@Provides
@ActivityScope
UserListView mUserListView(UserListFragment userListFragment){
return userListFragment;
}
我的模块也包含一些@Provides
方法.我们可以在同一模块中同时使用@Binds
和@Provides
方法吗?我尝试如下所示
My module contains some @Provides
methods as well. Can we use both @Binds
and @Provides
methods in the same module? I tried as shown below
@Module
public abstract class UserListModule {
@Provides
@ActivityScope
UserListFragment mUserListFragment() {
return new UserListFragment();
}
@Binds
@ActivityScope
abstract UserListView mUserListView(UserListFragment userListFragment);
// other provides and binds methods...
......
.....
}
其抛出错误
Error:(22, 8) error: dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
有什么办法吗?
推荐答案
@Binds
和@ContributesAndroidInjector
方法必须是抽象的,因为它们没有方法主体.这意味着它们必须进入接口或抽象类. @Provides
方法可能是static
,这意味着它们可以使用抽象类和Java-8编译的接口,但是非静态(实例")@Provides
方法不适用于抽象类.这在Dagger常见问题解答中的和.
@Binds
and @ContributesAndroidInjector
methods must be abstract, because they don't have method bodies. That means that they must go on an interface or abstract class. @Provides
methods may be static
, which means they can go on abstract classes and Java-8-compiled interfaces, but non-static ("instance") @Provides
methods don't work on abstract classes. This is explicitly listed in the Dagger FAQ, under the sections "Why can’t @Binds
and instance @Provides
methods go in the same module?" and "What do I do instead?".
如果您的@Provides
方法不使用实例状态,则可以将其标记为static
,它可以进入与@Binds
方法相邻的抽象类.如果不是,请考虑将像@Binds
和@ContributesAndroidInjector
这样的绑定放到一个单独的类中-可能是一个静态嵌套类-并使用Dagger的@Module
批注中的includes
属性将其包括在内.
If your @Provides
method doesn't use instance state, you can mark it static
, and it can go onto an abstract class adjacent to your @Binds
methods. If not, consider putting the bindings like @Binds
and @ContributesAndroidInjector
into a separate class--possibly a static nested class--and including that using the includes
attribute on Dagger's @Module
annotation.
这篇关于Dagger2:如何在同一模块中使用@Provides和@Binds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!