我不知道什么范围如下因素两个绑定有:GIN绑定的范围是什么?
bind(PermissionManager.class).in(Singleton.class);
和
bind(PermissionManager.class);
我已阅读JavaDoc中,他们表现如下。对于辛格尔顿:
/**
* Apply this to implementation classes when you want only one instance
* (per {@link Injector}) to be reused for all injections for that binding.
*
* @author [email protected] (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
*/
这是什么意思在实用性方面?每个客户端或每个JVM都是单身人士吗?对于没有范围,每个实例都不同?
感谢您的解释。所以在我的情况下,我有一个'PermissionManager',每个用户都拥有权限。它们对于与用户会话相关的所有注入应该是通用的(我不关心多个浏览器选项卡)。根据我的理解,“@ Singleton”是我需要的,对吧? – 2012-02-26 23:05:06
如果它本身持有状态(而不是从其他实例读取),那么很可能。您可能会发现使用'Provider'接口在实例准备就绪之前将其设置为null是方便的,并且将该提供程序作为单例绑定,但允许任何读取perms的请求都要求该提供程序使用'PermissionManager'。 (如果这是在服务器上,你可以使用@SessionScoped来替代,而Guice会想出如何保持会话特定的。)有多种方法可以正确执行此操作,具体取决于具体情况,以及何时数据,它的有效时间。 – 2012-02-27 02:23:11
哦,它在服务器上,不能相信我忘了提及这一点。看起来像'@ SessionScoped'就是我需要的。 – 2012-02-27 17:32:32