1

我在一些 GWT 应用程序中工作,其中我有一个层次结构,其中我有一个具有派生类的一些常见功能的抽象演示者。就像是:

public abstract class MyAbstractPresenter<T extends MyAbstractPresenter.CustomDisplay> extends Presenter<T>
{
public interface CustomDisplay extends View
{
  //some methods
}

//I want to inject this element
@Inject
private CustomObject myObj;

public MyAbstractPresenter(T display)
{
   super(display);
}
}

所有子类都被正确注入。但是,我希望能够注入该特定字段,而无需将其添加到子类的构造函数中。如您所见,我尝试进行字段注入,但它不起作用,因为它是被注入的子类。

是否有适当的方法来实现这种注入而不让子类知道该字段的存在?

4

1 回答 1

1

显然,就目前而言,GIN 中不支持这种行为。一种解决方法是在具体类构造函数中注入所需的字段,即使它们不需要它。就像是:

 public abstract class MyAbstractPresenter<T extends MyAbstractPresenter.CustomDisplay> extends Presenter<T>
 {
   public interface CustomDisplay extends View
   {
     //some methods
   }

   //I wanted to inject this element
   private final CustomObject myObj;

   public MyAbstractPresenter(T display, CustomObject obj)
   {
      super(display);
      myObj = obj;
   }
}

然后在任何扩展这个抽象实现的类中,我必须在构造时传递它。

public abstract class MyConcretePresenter extends MyAbstractPresenter<MyConcretePresenter.CustomDisplay>
{
  public interface CustomDisplay extends MyAbstractPresenter.CustomDisplay
  {
     //some methods
  }

 @Inject  //it would get injected here instead.
 public MyConcretePresenter(CustomDisplay display, CustomObject obj)
 {
     super(display, obj);
 }
}
于 2013-01-28T18:30:39.533 回答