Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gwt GIN绑定的范围是什么?_Gwt_Dependency Injection_Guice_Gwt Gin - Fatal编程技术网

Gwt GIN绑定的范围是什么?

Gwt GIN绑定的范围是什么?,gwt,dependency-injection,guice,gwt-gin,Gwt,Dependency Injection,Guice,Gwt Gin,我想知道以下两个绑定的作用域是什么: bind(PermissionManager.class).in(Singleton.class); 及 我已经阅读了JavaDocs,它们如下所示。单身人士: /** * Apply this to implementation classes when you want only one instance * (per {@link Injector}) to be reused for all injections for that binding.

我想知道以下两个绑定的作用域是什么:

bind(PermissionManager.class).in(Singleton.class);

我已经阅读了JavaDocs,它们如下所示。单身人士:

/**
* Apply this to implementation classes when you want only one instance
* (per {@link Injector}) to be reused for all injections for that binding.
*
* @author crazybob@google.com (Bob Lee)
*/
对于无范围:

/**
* No scope; the same as not applying any scope at all.  Each time the
* Injector obtains an instance of an object with "no scope", it injects this
* instance then immediately forgets it.  When the next request for the same
* binding arrives it will need to obtain the instance over again.
*
* <p>This exists only in case a class has been annotated with a scope
* annotation such as {@link Singleton @Singleton}, and you need to override
* this to "no scope" in your binding.
*
* @since 2.0
*/
/**
*没有范围;与根本不应用任何范围相同。每次
*Injector获得一个“无作用域”的对象实例,它将注入
*实例,然后立即忘记它。当下一个请求相同时
*绑定到达时,需要重新获取实例。
*
*只有在类被标注了作用域的情况下,才存在这种情况
*注释,如{@link Singleton@Singleton},您需要重写
*这将导致绑定中的“无作用域”。
*
*@自2.0以来
*/

这在实际中意味着什么?单例是每个客户端还是每个JVM?对于无作用域,每个实例都是不同的吗?

单例仅由您创建的顶级Ginject创建一次(对于大多数应用程序,您只创建一个Ginject)

每次将非作用域对象作为依赖项注入另一个对象时,都会创建这些对象。所以每一个例子都是不同的。如果您不打算定义绑定的范围(或者执行绑定器DSL允许您对绑定执行的任何其他操作),那么实际上不需要编写绑定(SomeClass.class)。只要拥有默认构造函数或向构造函数添加@Inject,Gin就可以创建它


一般来说,当你使用Guice或杜松子酒时,除非你有很好的理由不这样做,否则你想让事情不受影响。我建议您仔细阅读。

对于Gin,在实际应用中,当考虑每个客户端实例时,或者更准确地说,每个Ginject实例时,Singleton范围最有意义。如果您通过
GWT.create
ing一个实例两次而错误地创建了两个Ginjector,那么每个实例可能会有一个“单例”,因为每个Ginjector只能跟踪它所管理的实例

因此,每次加载应用程序时,它都会有自己的单例。如果用户在两个不同的选项卡中打开同一应用程序两次,则每个选项卡都有自己的单例。您可以将每个选项卡视为自己的JVM,因为它将具有自己的所有类的副本,并且不能在另一个窗口上通信或调用方法*。 对于无作用域,是,默认情况下每个实例都是不同的。当一个类型是
@Inject
ed时,它将是一个新实例,但是如果您
@Inject
a
Provider
作为一个字段,每次调用
get()
都可以得到一个新实例。这有助于轻松获取几个新注入的实例

在两种主要情况下使用单例是有意义的

  • 当实例持有需要在多个注入中共享的共享状态时——这是最常见的情况
  • 当实例的创建成本很高时——在服务器上,这通常是作为一个池来完成的,因此创建的对象不超过X个,但客户端上的昂贵对象往往是视图,有很多小部件,通常不需要多个小部件

*可以在另一个窗口上调用方法,但您需要使用浏览器的功能,通过发布消息,而不仅仅是来回传递对象。

谢谢您的解释。因此,在我的例子中,我有一个
PermissionManager
,它将持有每个用户的权限。它们对于与用户会话相关的所有注入都应该是通用的(我不太关心多个浏览器选项卡)。据我所知,
@Singleton
是我所需要的,对吗?如果它本身持有状态(而不是从其他实例读取),那么可能。您可能会发现,在实例准备就绪之前,使用
提供程序
接口将其设为空,并将该提供程序绑定为单例,但允许任何读取perms的内容向该提供程序请求
PermissionManager
。(如果这是在服务器上,您可能会使用@SessionScoped,Guice会找出如何保持其特定于会话的方式。)正确执行此操作的方法不止一种,具体取决于您的具体情况,以及当您有数据时,它的有效期有多长。哦,它在服务器上,真不敢相信我忘了提到这一点。看起来我需要的是
@SessionScoped
。您可能应该将问题标题/标记更改为指向Guice而不是Gin,因为Gin仅在客户端上。
/**
* No scope; the same as not applying any scope at all.  Each time the
* Injector obtains an instance of an object with "no scope", it injects this
* instance then immediately forgets it.  When the next request for the same
* binding arrives it will need to obtain the instance over again.
*
* <p>This exists only in case a class has been annotated with a scope
* annotation such as {@link Singleton @Singleton}, and you need to override
* this to "no scope" in your binding.
*
* @since 2.0
*/