Author: limpbizkit
Date: Wed Sep 24 13:02:58 2008
New Revision: 622
Modified:
wiki/Scopes.wiki
Log:
Edited wiki page through web user interface.
Modified: wiki/Scopes.wiki
==============================================================================
--- wiki/Scopes.wiki (original)
+++ wiki/Scopes.wiki Wed Sep 24 13:02:58 2008
@@ -4,19 +4,28 @@
Scopes are one of Guice's most powerful features.
+= Tips =
-= Best Practices =
+Scopes are applied to the binding source, not the binding target. For
example, suppose we have a class `Applebees` that implements both `Bar` and
`Grill` interfaces. These bindings will allow for *two* instances of that
type, one for `Bar`s and another for `Grill`s:
+{{{
+ bind(Bar.class).to(Applebees.class).in(Singleton.class);
+ bind(Grill.class).to(Applebees.class).in(Singleton.class);
+}}
+This is because the scopes apply to the bound type, not the type that
satisfies that binding. To allow only a single instance of our
implementation class to be created, use a [EMAIL PROTECTED] annotation on the
declaration for that class. Or add yet another binding:
+{{{
+ bind(Applebees.class).in(Singleton.class);
+}}
+This binding makes the other two `.in(Singleton.class)` clauses above
unnecessary.
Prefer to bind with the scope annotation rather than the scope instance:
{{{
// good:
- bind(Foo.class).in(ServletScopes.REQUEST);
+ bind(Foo.class).to(RealFoo.class).in(ServletScopes.REQUEST);
// better:
- bind(Bar.class).in(RequestScoped.class);
+ bind(Foo.class).to(RealFoo.class).in(RequestScoped.class);
}}}
-This way you can reuse the module in a non-servlets environment by
specifying a different scope to implement for `RequestScoped.class`
-
+This way you can reuse the module in a non-servlets environment by
specifying a different scope to implement for `RequestScoped.class`. Even
better, use the [EMAIL PROTECTED] annotation on the implementation class.
= Custom Scopes =
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---