Java GWT延迟绑定和依赖注入
假设我有这样的东西 接口Java GWT延迟绑定和依赖注入,java,gwt,dependency-injection,Java,Gwt,Dependency Injection,假设我有这样的东西 接口 Interface IsInterface {} 实现类 public ConcreteClassA implements IsInterface { InjectorA a, InjectorB c, InjectorC c; @Inject ConcreteClassA(InjectorA a, InjectorB b, InjectorC c) { this.a = a; this.b = b; ..... } } public
Interface IsInterface {}
实现类
public ConcreteClassA implements IsInterface {
InjectorA a, InjectorB c, InjectorC c;
@Inject
ConcreteClassA(InjectorA a, InjectorB b, InjectorC c) {
this.a = a;
this.b = b;
.....
}
}
public ConcreteClassB implements IsInterface {
InjectorA a, InjectorB B, InjectorC C;
@Inject
ConcreteClassB(InjectorA a, InjectorB b, InjectorC c) {
this.a = a;
this.b = b;
.....
}
}
…然后我决定在我的GWT module.GWT.xml中使用GWT延迟绑定
//Pseudo XML Configuration
if (type is IsInterface)
if toggle == A then use ConcreteClass A else use ConcreteClassB
现在,当我试着运行这个。它不会工作,因为GWT希望我的具体类A和B具有默认的0构造函数。因此,我在我的混凝土课堂上尝试了以下内容
@Inject
InjectorA a;
@Inject
InjectorB b;
@Inject
InjectorC c;
ConcreteClassA() {
}
它绕过了0构造函数错误,但当我尝试使用a、b或c时,它会给我NullPointerException。让它工作的一种方法是删除@Inject并像这样使用GWT.create()
这将起作用,但如果我的InjectorA、InjectorB和InjectorC没有0构造函数,并且严重依赖@Injecte,该怎么办。我不想遍历所有类来创建0个构造函数,并用GWT.create()替换@inject。必须有更好的方法来做到这一点。我在这里遗漏了什么吗?找到了解决方案
interface IsInterface {
@Inject
void init(InjectorA a, InjectorB b, ...);
}
public ConcreteClassA implements IsInterface {
InjectorA a, InjectorB c, InjectorC c;
ConcreteClassA() {}
@Override
public void init(InjectorA a, InjectorB b, ..) {
this.a = a;
this.b = b;
....
}
}
可能重复的
interface IsInterface {
@Inject
void init(InjectorA a, InjectorB b, ...);
}
public ConcreteClassA implements IsInterface {
InjectorA a, InjectorB c, InjectorC c;
ConcreteClassA() {}
@Override
public void init(InjectorA a, InjectorB b, ..) {
this.a = a;
this.b = b;
....
}
}