Author: limpbizkit
Date: Sun Mar 1 13:44:12 2009
New Revision: 881
Added:
wiki/JustInTimeBindings.wiki
Log:
Created wiki page through web user interface.
Added: wiki/JustInTimeBindings.wiki
==============================================================================
--- (empty file)
+++ wiki/JustInTimeBindings.wiki Sun Mar 1 13:44:12 2009
@@ -0,0 +1,47 @@
+#summary Bindings that are created automatically by Guice
+=Just-in-time Bindings=
+When the injector needs an instance of a type, it needs a binding. The
[CreatingBindings bindings in a modules] are called *explicit bindings*,
and the injector uses them whenever they're available. If a type is needed
but there isn't an explicit binding, the injector will attempt to create a
*Just-In-Time binding*. These are also known as JIT bindings and implicit
bindings.
+
+==Constructor Bindings==
+Guice can create bindings for concrete types by using the type's
*injectable constructor*. This is either a public, no-arguments
constructor, or a constructor with the `...@inject` annotation:
+{{{
+public class PayPalCreditCardProcessor implements CreditCardProcessor {
+ private final String apiKey;
+
+ @Inject
+ public PayPalCreditCardProcessor(@Named("PayPal API key") String apiKey)
{
+ this.apiKey = apiKey;
+ }
+}}}
+Guice will not construct nested classes unless they have the `static`
modifier. Inner classes have an implicit reference to their enclosing class
that cannot be injected.
+
+...@implementedby==
+Annotate types tell the injector what their default implementation type
is. The `...@implementedby` annotation acts like a *linked binding*,
specifying the subtype to use when building a type.
+{{{
+...@implementedby(PayPalCreditCardProcessor.class)
+public interface CreditCardProcessor {
+ ChargeResult charge(String amount, CreditCard creditCard)
+ throws UnreachableException;
+}
+}}}
+The above annotation is equivalent to the following `bind()` statement:
+{{{
+ bind(CreditCardProcessor.class).to(PayPalCreditCardProcessor.class);
+}}}
+If a type is in both a `bind()` statement (as the first argument) and has
the `...@implementedby` annotation, the `bind()` statement is used. The
annotation suggests a _default implementation_ that can be overridden with
a binding. Use `...@implementedby` carefully; it adds a compile-time
dependency from the interface to its implementation.
+
+...@providedby==
+...@providedby` tells the injector about a `Provider` class that produces
instances:
+{{{
+...@providedby(DatabaseTransactionLogProvider.class)
+public interface TransactionLog {
+ void logConnectException(UnreachableException e);
+ void logChargeResult(ChargeResult result);
+}
+}}}
+The annotation is equivalent to a `toProvider()` binding:
+{{{
+ bind(TransactionLog.class)
+ .toProvider(DatabaseTransactionLogProvider.class);
+}}}
+Like `...@implementedby`, if the type is annotated and used in a `bind()`
statement, the `bind()` statement will be used.
\ No newline at end of file
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---