【发布时间】:2016-04-26 18:32:58
【问题描述】:
我正在玩 Dagger2 以了解它是如何工作的。我刚刚创建了一个“hello dagger2”基本项目,但它崩溃了
我有三个课程:面包、面粉和水。面包依赖于面粉和水。
类面包:
public class Bread {
private Water water;
private Flour flour;
@Inject
public Bread (Water water, Flour flour){
this.water = water;
this.flour = flour;
}
}
类水:
public class Water {
int waterQuantity;
public Water(int waterQuantity){
this.waterQuantity = waterQuantity;
}
}
面粉类:
public class Flour {
private int flourQuantity;
public Flour(int flourQuantity){
this.flourQuantity = flourQuantity;
}
}
我也实现了模块和组件
模块:
@Module
public class BreadModule {
@Provides @Singleton
Bread provideBread(Water water, Flour flour){
return new Bread(water, flour);
}
}
评论:
@Singleton
@Component (modules = {BreadModule.class})
public interface BreadComponent {
Bread getBread();
}
我面临的错误是:
错误:(13, 11) 错误:com.example.llisas.testingdagger2.model.Water 如果没有 @Inject 构造函数或从 @提供带注释的方法。 com.example.llisas.testingdagger2.module.BreadModule.provideBread(com.example.llisas.testingdagger2.model.Water 水,com.example.llisas.testingdagger2.model.Flour 面粉) [参数:com.example.llisas.testingdagger2.model.Water 水]
我做错了什么?
【问题讨论】: