【发布时间】:2016-02-08 20:32:15
【问题描述】:
我正在尝试在 Dagger 2 中确定子组件的范围,但在构建时出现以下错误
cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
如果我注释范围注释,则构建成功。 有谁知道为什么? Dagger 2 不是为这种情况设计的吗?
父组件.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);
}
}
【问题讨论】:
-
我刚刚复制了那个代码。它编译得很好。范围上有和没有 cmets。
-
感谢大卫运行它。你是对的,我将这段代码隔离到它自己的应用程序中并构建。我想我必须进一步调试。
标签: dagger-2