繁体   English   中英

GWT延迟绑定和依赖注入

[英]GWT Deferred Binding and 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 ConcreteClassB implements IsInterface {

 InjectorA a, InjectorB B, InjectorC C;

 @Inject
 ConcreteClassB(InjectorA a, InjectorB b, InjectorC c) {
   this.a = a;
   this.b = b;
   .....
 }

}

..然后我决定使用GWT延迟绑定,因此在我的GWT module.gwt.xml中

//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 a, InjectorB b, InjectorC c;

 ConcreteClassA() {
   this.a = GWT.create(InjectorA.class);
   .....
   .....
 }

这将起作用,但是如果我的InjectorA,InjectorB和InjectorC没有0构造函数并且严重依赖@inject怎么办? 我不想遍历所有类以创建0构造函数并将@inject替换为GWT.create()。 必须有一个更好的方法来做到这一点。 我在这里想念什么吗?

找到了解决方案

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;
  ....
 }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM