1

这是我的问题。我在 gwt 项目中使用 Gin,我使用 GWT.create(SomeClass.class) 来获取实例,但问题是我想要 signleton 实例,为此我将 app 模块中的该类绑定为单例。我执行的每一本书 GWT.create(TemplatePanel.class) 它返回不同的实例..为什么?这是我的代码片段。模块

public class AppClientModule extends AbstractGinModule
{
protected void configure()
{
  bind(MainPanel.class).in(Singleton.class);
  bind(TemplatePanel.class).in(Singleton.class);
}
} 

注射器

@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
  MainPanel getMainForm();
  TemplatePanel getTemplateForm();
}

模板面板

public class TemplatePanel extends VerticalPanel
@Inject
public TemplatePanel()
{
  this.add(initHeader());
  this.add(initContent());
}
..

主面板

public void onSuccess(List<MyUser> result)
    {
.......
  TemplatePanel temp = GWT.create(TemplatePanel.class);

.......
}

和入口点

private final AppInjector injector = GWT.create(AppInjector.class);
public void onModuleLoad()
{
  MainPanel mf = injector.getMainForm();
  TemplatePanel template = injector.getTemplateForm();
  template.setContent(mf);
  RootPanel.get().add(template);
}
4

2 回答 2

7

GWT.create(..)不适用于 GIN,它只是以普通 GWT 方式创建一个对象。您应该:

  1. 注入TemplatePanel,MainPanel

  2. 实例化注入器(可能通过静态方法),然后获取TemplatePanel.

我通常对注入器有一个静态引用(因为每个应用程序只需要一个),所以我可以在任何地方访问它:

@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
    AppInjector INSTANCE = GWT.create(AppInjector.class);

    MainPanel getMainForm();
    TemplatePanel getTemplateForm();
}

(注意:常量接口字段根据定义是公共的和静态的,因此您可以省略它们。)

然后你会使用:

TemplatePanel temp = AppInjector.INSTANCE.getTemplateForm();
于 2011-06-02T11:34:04.267 回答
0

GWT.create 只是调用 new XXX ,其中 XXX 是您传递给它的类文字。然而,当 XXX 类在为其定义的模块中有一些规则时,它确实会产生一些魔力,也就是延迟绑定。我 fyou 可以 Gwt.create(YYY) 并且有一条规则说如果用户代理是 Internet Explorer 2 使用 ZZZ 那么它会。

于 2013-07-04T01:42:45.753 回答