我不知道什麼範圍如下因素兩個綁定有: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