使用GIN在GWT中注入入口点类 [英] Inject entry point class in GWT with GIN

查看:182
本文介绍了使用GIN在GWT中注入入口点类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  @Singleton 
公共类AAA实现了EntryPoint,HistoryListener
{

私人BBB bbb;
私人CCC ccc;
私人DDD ddd;
私人EEE eee;

@Inject
公共AAA(BBB bbb,CCC ccc,DDD ddd,EEE eee)
{
this.bbb = bbb;
this.ccc = ccc;
this.ddd = ddd;
this.eee = ee;
}
.........
}

结果对所有实例都是空的。
我期望这种方式可以工作...



我知道我可以做这样的事情,例如

  private final MyGinjector injector = GWT.create(MyGinjector.class); 

injector.getAAA()
等等。

为什么我尝试的第一种方法对我不起作用?
有何建议?

谢谢

解决方案

你可以使用Guice的 injectMembers 特性,它在GIN中是通过在你的Ginjector中声明一个方法来获得一个参数来完成的。

  @GinModules(...)
接口MyGinjector扩展了Ginjector {

public void injectEntryPoint(AAA入口点) ;

...
}

公共类AAA实现EntryPoint {
@Inject private BBB bbb; //域注入工程
私有CCC ccc;

@Inject void setCcc(CCC ccc){this.ccc = ccc; } //当然还有方法注入

public onModuleLoad(){
MyGinjector injector = GWT.create(MyGinjector.class);
injector.injectEntryPoint(this);
...
}
}

顺便说一句,你不要不需要用 @Singleton 注释EntryPoint,除非你将它注入到另一个类中(并且你不得不求助于hack将它绑定到由GWT创建的实例,而不是GIN创建它自己的),你的应用中只有一个EntryPoint实例。


I've tried to do something like this:

@Singleton
public class AAA implements EntryPoint, HistoryListener
{

private BBB bbb;
private CCC ccc;
private DDD ddd;
private EEE eee;

@Inject
public AAA(BBB bbb, CCC ccc, DDD ddd, EEE eee)
{
  this.bbb = bbb;
  this.ccc = ccc;
  this.ddd = ddd;
  this.eee = ee;
}
.........
}

The result is null to all instances.. I expected this way to work...

I know i could do something like this for example

private final MyGinjector injector = GWT.create(MyGinjector.class);

injector.getAAA()
and so on..

Why the first way i have tried does not work for me? Any suggestions?

Thanks

解决方案

You can use the injectMembers feature of Guice, which in GIN is done by declaring a method in your Ginjector taking a single argument.

@GinModules(...)
interface MyGinjector extends Ginjector {

   public void injectEntryPoint(AAA entryPoint);

   ...
}

public class AAA implements EntryPoint {
   @Inject private BBB bbb; // field injection works
   private CCC ccc;

   @Inject void setCcc(CCC ccc) { this.ccc = ccc; } // and of course method injection

   public onModuleLoad() {
      MyGinjector injector = GWT.create(MyGinjector.class);
      injector.injectEntryPoint(this);
      ...
   }
}

BTW, you don't need to annotate your EntryPoint with @Singleton: unless you inject it into another class (and you'd have to resort to hacks to bind it to the instance created by GWT, and not have GIN create its own), you'll only have a single EntryPoint instance in your app.

这篇关于使用GIN在GWT中注入入口点类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆