我正在尝试在Dagger 2中确定子组件的范围,但在构建
时会出现以下错误cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
如果我评论范围注释,则构建成功。 谁知道为什么? Dagger 2不适用于这种情况吗?
ParentComponent.java
import com.cueyoueye.android.inject.annotation.ActivityScope;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.Subcomponent;
@Singleton
@Component(modules = ParentComponent.ParentModule.class)
public interface ParentComponent {
SubComponent subComponent();
// @ActivityScope
@Subcomponent(modules = SubComponentModule.class)
interface SubComponent {
void inject(Example activity);
}
@Module
class SubComponentModule {
@Provides
// @ActivityScope
B provideB() {
return new B("Better");
}
}
class B {
public B(String s) {
}
}
@Module
class ParentModule {
@Provides
@Singleton
Ab provideAb() {
return new Ab("s");
}
}
@Singleton
class A {
@Inject
public A() {
}
}
class Ab {
public Ab(String s) {
}
}
}
Example.java
import javax.inject.Inject;
public class Example {
@Inject
ParentComponent.B b;
public void injectSelf() {
DaggerParentComponent.builder()
.build()
.subComponent()
.inject(this);
}
}
答案 0 :(得分:0)
此代码在完全隔离中起作用。其他因素导致构建错误。