Java Dagger 2:没有@Provides-annotated 方法就不能提供

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43116825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:42:30  来源:igfitidea点击:

Dagger 2: Cannot be provided without an @Provides-annotated method

javaandroiddependency-injectiondagger-2dagger

提问by hanchen ke

I just started learning dagger2 and faced strange issue that looks like a bug to me. Here's the module:

我刚开始学习 dagger2 并遇到了对我来说看起来像是一个错误的奇怪问题。这是模块:

@Module
public class SimpleModule {

    @Provides
    Cooker providerCooker() {

        return new Cooker("tom", "natie");
    }
}

Component:

成分:

@Component(modules = SimpleModule.class)
public interface SimpleComponent {

    void inject(DaggerTestActivity activity);

}

Interface:

界面:

public interface CoffeeMaker {

    String makeCoffee();
}

Implementation:

执行:

  public class SimpleMaker implements CoffeeMaker {

        Cooker mCooker;

        @Inject
        public SimpleMaker(Cooker cooker) {

            this.mCooker = cooker;

        }

        @Override
        public String makeCoffee() {

            return mCooker.makeCoffee();
        }
    }

Cooker :

电饭煲:

public class Cooker {

    String name; 
    String coffeeKind;


    public Cooker(String name, String coffeeKind) {
        this.name = name;
        this.coffeeKind = coffeeKind;
    }

   public  String  makeCoffee() {

        return name + "make" + coffeeKind; 
    }

}

Coffee machine:

咖啡机:

public class CoffeeMachine {

    CoffeeMaker mMaker;

    @Inject
    public CoffeeMachine(CoffeeMaker coffeeMaker) {

        this.mMaker = coffeeMaker;
    }

    public String makeCoffee() {

        return mMaker.makeCoffee();
    }
}

Just it. I use in the activit. Faced strange issue here:

只是它。我在活动中使用。在这里遇到奇怪的问题:

    @Inject
    CoffeeMachine mCoffeeMachine;

Error I'm getting from the Dagger 2 compiler is the following:

我从 Dagger 2 编译器得到的错误如下:

Error:(14, 10) com.wyyc.daggertest.CoffeeMaker cannot be provided without an @Provides-annotated method.
com.wyyc.zqqworkproject.DaggerTestActivity.mCoffeeMachine
[injected field of type: com.wyyc.daggertest.CoffeeMachine mCoffeeMachine]
com.wyyc.daggertest.CoffeeMachine.<init>(com.wyyc.daggertest.CoffeeMaker coffeeMaker)

All this situation looks very strange, and I'd like to hear some input from more experienced Dagger 2 users.

所有这些情况看起来都很奇怪,我想听听更有经验的 Dagger 2 用户的一些意见。

采纳答案by azizbekian

Your CoffeeMachineneeds CoffeeMaker. And you have declared that Dagger will take care of providing that dependency to the CoffeeMachineby annotating the constructor with @Inject. But Dagger says:

您的CoffeeMachine需求CoffeeMaker。并且您已经声明 Dagger 将CoffeeMachine通过使用 注释构造函数来负责向 提供该依赖项@Inject。但是匕首说:

CoffeeMaker cannot be provided without an @Provides-annotated method

如果没有@Provides 注释的方法,则无法提供 CoffeeMaker

Because you haven't specified anywhere how CoffeeMakerobject should be created. @Injecting SimpleMakeris not enough, because SimpleMaker!= CoffeeMaker. So, you have to specify explicitly, that when Dagger wants CoffeeMakerthen provide him SimpleMaker.

因为您没有在任何地方指定CoffeeMaker应该如何创建对象。@InjectingSimpleMaker是不够的,因为SimpleMaker!= CoffeeMaker。因此,您必须明确指定,当 Dagger 想要时CoffeeMaker再提供他SimpleMaker

Change your module to this:

将您的模块更改为:

@Module
public class SimpleModule {

    @Provides
    Cooker providerCooker() {
        return new Cooker("tom", "natie");
    }

    @Provides
    CoffeeMaker provideCoffeeMaker(Cooker cooker) {
        return new SimpleMaker(cooker);
    }

}