【问题标题】:GWT Deferred Binding and Dependency InjectionGWT 延迟绑定和依赖注入
【发布时间】:2013-08-24 03:27:52
【问题描述】:

假设我有类似的东西

界面

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

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

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

【问题讨论】:

标签: java gwt dependency-injection


【解决方案1】:

找到了解决办法

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-12
    • 2018-02-20
    • 1970-01-01
    • 2013-08-09
    • 2014-12-17
    • 2014-09-11
    • 2012-02-07
    • 2013-07-08
    相关资源
    最近更新 更多