Dagger2:当两个组件具有相同的注入方法签名时出错
Dagger2: Error when two components has same inject method signature
我有这个组件:
@Singleton
@Component(modules = OauthModule.class)
public interface OauthComponent {
void inject(LoginActivity a);
}
和模块:
@Module
public class OauthModule {
@Provides
@Singleton
Oauth2Service provideOauth2Service() {
return new Oauth2StaticService();
}
}
这是另一个组件:
@Singleton
@Component(modules = LoggedUserModule.class)
public interface LoggedUserComponent {
void inject(LoginActivity a);
}
我得到这个错误:
Error:(15, 10) error: Oauth2Service cannot be provided without an
@Provides- or @Produces-annotated method.
如果我将 LoggedUserComponent
的注入方法参数更改为另一个 Activity
,请这样说 AnotherActivity
:
@Singleton
@Component(modules = LoggedUserModule.class)
public interface LoggedUserComponent {
void inject(AnotherActivity a);
}
编译正常。为什么?我不能有两个具有相同注入签名的组件吗?
我正在尝试了解 Dagger
的工作原理,因此我们将不胜感激。谢谢。
它变得很生气,因为你说你可以注入那个 class 但你没有提供它期望你提供的 class。您只需将 OauthModule 添加到您的 LoggedUserComponent。试试这个
@Singleton
@Component(modules = {LoggedUserModule.class, OauthModule.class})
public interface LoggedUserComponent {
void inject(LoginActivity loginActivity);
}
将 dagger
视为一个对象图——它实际上是。你可能应该 不 有 2 个不同的组件能够注入同一个对象,而不是出于测试目的(或者如果你想包含不同的行为,而不是额外的行为)。
如果你的 LoginActivity
依赖于多个模块,你应该将它们聚合在一个组件中,因为正如你的错误所示,如果 dagger 不能提供 all[=31= 就会失败] 来自单个组件的依赖项。
@Singleton
@Component(modules = {LoggedUserModule.class, OauthModule.class})
public interface LoggedUserComponent {
void inject(AnotherActivity a);
}
看看 Oauth2Service
,这很容易成为多个对象可以使用的东西,因此更高的范围就足够了。在这种情况下,您应该考虑将其与 @Singleton
作用域一起添加到您的应用程序组件中,或者创建自己的组件,例如@UserScope
.
然后您必须将您的 LoggedUserComponent
设为 @Subcomponent
或使用 @Component(dependencies = OauthComponent.class)
并在 OauthComponent
中提供 getter 声明此组件为依赖项为了它。在这两种情况下,dagger 还能够提供图中更高的依赖关系,从而也解决了您的错误。
我有这个组件:
@Singleton
@Component(modules = OauthModule.class)
public interface OauthComponent {
void inject(LoginActivity a);
}
和模块:
@Module
public class OauthModule {
@Provides
@Singleton
Oauth2Service provideOauth2Service() {
return new Oauth2StaticService();
}
}
这是另一个组件:
@Singleton
@Component(modules = LoggedUserModule.class)
public interface LoggedUserComponent {
void inject(LoginActivity a);
}
我得到这个错误:
Error:(15, 10) error: Oauth2Service cannot be provided without an @Provides- or @Produces-annotated method.
如果我将 LoggedUserComponent
的注入方法参数更改为另一个 Activity
,请这样说 AnotherActivity
:
@Singleton
@Component(modules = LoggedUserModule.class)
public interface LoggedUserComponent {
void inject(AnotherActivity a);
}
编译正常。为什么?我不能有两个具有相同注入签名的组件吗?
我正在尝试了解 Dagger
的工作原理,因此我们将不胜感激。谢谢。
它变得很生气,因为你说你可以注入那个 class 但你没有提供它期望你提供的 class。您只需将 OauthModule 添加到您的 LoggedUserComponent。试试这个
@Singleton
@Component(modules = {LoggedUserModule.class, OauthModule.class})
public interface LoggedUserComponent {
void inject(LoginActivity loginActivity);
}
将 dagger
视为一个对象图——它实际上是。你可能应该 不 有 2 个不同的组件能够注入同一个对象,而不是出于测试目的(或者如果你想包含不同的行为,而不是额外的行为)。
如果你的 LoginActivity
依赖于多个模块,你应该将它们聚合在一个组件中,因为正如你的错误所示,如果 dagger 不能提供 all[=31= 就会失败] 来自单个组件的依赖项。
@Singleton
@Component(modules = {LoggedUserModule.class, OauthModule.class})
public interface LoggedUserComponent {
void inject(AnotherActivity a);
}
看看 Oauth2Service
,这很容易成为多个对象可以使用的东西,因此更高的范围就足够了。在这种情况下,您应该考虑将其与 @Singleton
作用域一起添加到您的应用程序组件中,或者创建自己的组件,例如@UserScope
.
然后您必须将您的 LoggedUserComponent
设为 @Subcomponent
或使用 @Component(dependencies = OauthComponent.class)
并在 OauthComponent
中提供 getter 声明此组件为依赖项为了它。在这两种情况下,dagger 还能够提供图中更高的依赖关系,从而也解决了您的错误。