开发者

Dependency injection not working in gwt 2.1

I have a new project where I am using GWT-Views like Composite, etc.

I have injected the items in the main menu (like ProductList below) using GinInjector. This works fine!

Somewhere I want to have a reference from a small component to an item from my main menu in order to update it. I try to inject it this way:

public class ProductForm extends Composite {
   ...
   @Inject 
   ProductList list;
   ....

}

But when I use the list I always get null. Whereby, ProductList is defined this way:

public class MyModule extends AbstractGinModule {
   ...
   @Override
   protected void configure() {
      bind(ProductList.class).asEagerSingleton();
      bind(ProductForm.class).asEagerSingleton();
   }
   ...
}

Any idea what I am doing wrong?!

So开发者_C百科lution: I failed to mention that ProductForm is an element of the ProductList using the UIBinder's @UIField tag, So That injecting it will create a new object rather than the one created using UIBinder.

I had to restructure my code to include presenters and an event bus so that no direct references between views are needed (other than the @UIField attributes).


I was working through Gin documentation : I'll quote it here :

Gin "Magic" Gin tries to make injection painless and remove as much boilerplate from your code as possible. To do that the generated code includes some magic behind the scenes which is explained here.

Deferred Binding One way Gin optimizes code is by automating GWT deferred binding. So if you inject an interface or class1 bound through deferred binding (but not through a Guice/Gin binding), Gin will internally call GWT.create on it and inject the result. One example are GWT messages and constants (used for i18n purposes):

public interface MyConstants extends Constants {
  String myWords();
}

public class MyWidget {

  @Inject
  public MyWidget(MyConstants myconstants) {
    // The injected constants object will be fully initialized - 
    // GWT.create has been called on it and no further work is necessary.
  }
}

Note: Gin will not bind the instances created through GWT.create in singleton scope. That should not cause unnecessary overhead though, since deferred binding generators usually implement singleton patterns in their generated code.

You can see for yourself in this URL : http://code.google.com/p/google-gin/wiki/GinTutorial

It fails to mention why a singleton cannot be autogenerated by deferred binding and injected.

You can fix this by handcreating, using GWT.create(YourFactoryInterface.class).getProductList() in the constructor.

This means for testing puposes, you will need to pull the GWT.create into a seperate method and override it in a subclass and use that for testing like :

YourFactoryInterface getFactory() {
  return GWT.create(YourFactoryInterface.class)
}

and

getFactory().getProductList()


Gin's asEagerSingleton() binding was broken for quite a while, and was injecting null. Not sure when the fix went in, but I had problems with eager singletons on v1.0. See the issue or the explanation, if you're interested. I'd either switch to regular .in(Singleton.class) bindings, or make sure you're using Gin 1.5.


Is the Ginjector creating the ProductForm? I think maybe that is needed to make it populate the injected variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜