【问题标题】:Why does Dagger-2 subcomponent scope not work?为什么 Dagger-2 子组件范围不起作用?
【发布时间】: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


【解决方案1】:

此代码完全独立运行。其他原因导致该构建错误。

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多