[英]Dagger2 Error at inject in a class who injects
I'm trying to inject
in my presenter
an interactor
but gives me an error, at seems I can't inject in a class who injects to another: 我试图在presenter
inject
一个interactor
但给我一个错误,看来我无法在一个注入另一个的类中注入:
error: [Dagger/DuplicateBindings] com.example.calculadora.Data.Interactor.Operacion is bound multiple times:
@Provides com.example.calculadora.Data.Interactor.Operacion com.example.calculadora.Inject.InteractorModule.provideDiv()
@Provides com.example.calculadora.Data.Interactor.Operacion com.example.calculadora.Inject.InteractorModule.provideMult()
@Provides com.example.calculadora.Data.Interactor.Operacion com.example.calculadora.Inject.InteractorModule.provideResta()
@Provides com.example.calculadora.Data.Interactor.Operacion com.example.calculadora.Inject.InteractorModule.provideSuma()
com.example.calculadora.Data.Interactor.Operacion is injected at
com.example.calculadora.Domain.PresenterImpl.operacion
com.example.calculadora.Domain.PresenterImpl is injected at
com.example.calculadora.Inject.InteractorComponent.inject(com.example.calculadora.Domain.PresenterImpl)
This is my InteractorModule
who provides me a 4 classes depending wich I want to use and where seems is the problem: 这是我的InteractorModule
,它为我提供了4个类,具体取决于我要使用的类,问题出在哪里:
@Module
public class InteractorModule {
@Provides
public Operacion provideSuma() {
return new InteractorSuma();
}
@Provides
public Operacion provideResta() {
return new InteractorResta();
}
@Provides
public Operacion provideDiv() {
return new InteractorDivision();
}
@Provides
public Operacion provideMult() {
return new InteractorMultiplicacion();
}
}
I want to inject here instead of init the new items: 我想在这里注入而不是初始化新项目:
@Override
public void setCalculo() {
Operacion operacion = null;
String[] operandos = vista.getOperandos();
Operando operando1 = new Operando(Integer.parseInt(operandos[0]));
Operando operando2 = new Operando(Integer.parseInt(operandos[1]));
switch (tipoOperacion) {
case SUMA:
operacion = new InteractorSuma(operando1, operando2);
break;
case RESTA:
operacion = new InteractorResta(operando1, operando2);
break;
case MULT:
operacion = new InteractorMultiplicacion(operando1, operando2);
break;
case DIV:
operacion = new InteractorDivision(operando1, operando2);
break;
}
operacion.calcular();
vista.mostrarResultado(String.valueOf(operacion.getResultado().getValor()));
}
For this kind of situation is where Dagger2 Qualifiers
are build for. 对于这种情况,需要构建Dagger2 Qualifiers
。
1.- Create your qualifiers: 1.-创建您的限定词:
@Qualifier
public @interface OperacionSuma {}
@Qualifier
public @interface OperacionResta {}
@Qualifier
public @interface OperacionDiv {}
@Qualifier
public @interface OperacionMult {}
2.- Set qualifiers in your providers methods: 2.-在您的提供者方法中设置限定词:
@Module
public class InteractorModule {
@Provides
@OperacionSuma
public Operacion provideSuma() {
return new InteractorSuma();
}
@Provides
@OperacionResta
public Operacion provideResta() {
return new InteractorResta();
}
@Provides
@OperacionDiv
public Operacion provideDiv() {
return new InteractorDivision();
}
@Provides
@OperacionMult
public Operacion provideMult() {
return new InteractorMultiplicacion();
}
}
3.- Specify what kind of "operation" do you want to inject in your presenter: 3.-指定要在演示者中注入的“操作”类型:
class Presenter {
@Inject
Presenter(@OperacionSuma Operacion operacion) { }
@Inject
Presenter(@OperacionResta Operacion operacion) { }
@Inject
Presenter(@OperacionDiv Operacion operacion) { }
@Inject
Presenter(@OperacionMult Operacion operacion) { }
}
You should separate one-other with the @Named("someName")
annotation, or you could just do what @Derek says. 您应该使用@Named("someName")
注释分隔另一个,或者您可以按照@Derek的说明进行操作。 My approach: 我的方法:
@Provides
@Named("someName1")
public Operacion provideSuma() {
return new InteractorSuma();
}
@Provides
@Named("someName2")
public Operacion provideResta() {
return new InteractorResta();
}
@Provides
@Named("someName3")
public Operacion provideDiv() {
return new InteractorDivision();
}
@Provides
@Named("someName4")
public Operacion provideMult() {
return new InteractorMultiplicacion();
}
Otherwise dagger doesn't know which one to return where. 否则,匕首将不知道该返回哪一个。
Call the @Named
when injecting also. 注入时也调用@Named
。
Since dagger looks for the return type, not a name that given to the function you should take care of it. 由于dagger寻找返回类型,而不是函数的名称,因此您应该注意它。 However, Dagger2 provides a solution to such problems. 但是,Dagger2提供了解决此类问题的方法。 Using @Named annotation. 使用@Named注释。
Sometimes the type alone is insufficient to identify a dependency. 有时,仅类型本身不足以识别依赖项。 For example, if you need a Refrofit instance with GsonConverterFactory and another one ScalarConverterFactory you will end up with 2 provide methods that have the same return type: Retrofit. 例如,如果您需要一个带有GsonConverterFactory的Refrofit实例和另一个ScalarConverterFactory的Refrofit实例,您将最终得到2个提供具有相同返回类型的方法:Retrofit。 In this case, you can use @Named annotation to differentiate two Retrofit instances 在这种情况下,您可以使用@Named批注来区分两个Retrofit实例
Now you can use it like following 现在您可以像下面那样使用它
Coming to your case 来你的情况
@Module
public class InteractorModule {
@Provides
@Named("InteractorSuma")
public Operacion provideSuma() {
return new InteractorSuma();
}
@Provides
@Named("InteractorResta")
public Operacion provideResta() {
return new InteractorResta();
}
@Provides
@Named("InteractorDivision")
public Operacion provideDiv() {
return new InteractorDivision();
}
@Provides
@Named("InteractorMultiplicacion")
public Operacion provideMult() {
return new InteractorMultiplicacion();
}
}
Here is the full example of how to use @Named
annotation 这是如何使用@Named
注释的完整示例
Let me know if you have still a problem 让我知道你是否还有问题
Return the instance of Child class, not parent class. 返回子类的实例,而不是父类的实例。
@Module
public class InteractorModule {
@Provides
public InteractorSuma provideSuma() {
return new InteractorSuma();
}
@Provides
public InteractorResta provideResta() {
return new InteractorResta();
}
@Provides
public InteractorDivision provideDiv() {
return new InteractorDivision();
}
@Provides
public InteractorMultiplicacion provideMult() {
return new InteractorMultiplicacion();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.