[英]Gin problem using GWT - GWT.create(someClass.class) return different instance every time is called
这是我的问题。 I m using Gin in a gwt project, im using GWT.create(SomeClass.class) to get instance,but the problem is that i want signleton instance and for that purpose i bind that class in the app module to be singleton. 每本书我执行 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);
}
GWT.create(..)
不适用于 GIN,它只是以正常的 GWT 方式创建 object。 您应该:
在MainPanel
中注入TemplatePanel
,或
实例化注入器(可能通过 static 方法),然后获取TemplatePanel
。
我通常有一个 static 参考注射器(因为每个应用程序只需要一个)所以我可以在任何地方访问它:
@GinModules(AppClientModule.class)
public interface AppInjector extends Ginjector
{
AppInjector INSTANCE = GWT.create(AppInjector.class);
MainPanel getMainForm();
TemplatePanel getTemplateForm();
}
(注意:常量接口字段定义为 public 和 static,因此您可以省略它们。)
然后你会使用:
TemplatePanel temp = AppInjector.INSTANCE.getTemplateForm();
GWT.create 只需调用 new XXX ,其中 XXX 是您传递给它的 class 文字。 然而,当 XXX class 在为其定义的模块中具有一些规则时,它确实有一些魔力,也就是延迟绑定。 如果你可以 Gwt.create(YYY) 并且有一条规则说如果用户代理是 Internet Explorer 2 使用 ZZZ 那么它会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.