Author: limpbizkit
Date: Mon Mar  2 20:10:00 2009
New Revision: 887

Added:
    wiki/Injections.wiki

Log:
Created wiki page through web user interface.

Added: wiki/Injections.wiki
==============================================================================
--- (empty file)
+++ wiki/Injections.wiki        Mon Mar  2 20:10:00 2009
@@ -0,0 +1,93 @@
+#summary How Guice initializes your objects
+=Injections=
+The dependency injection pattern separates behaviour from dependency  
resolution. Rather than looking up dependencies directly or from factories,  
the pattern recommends that dependencies are passed in. The process of  
setting dependencies into an object is called *injection*.
+
+==Constructor Injection==
+Constructor injection combines instantiation with injection. To use it,  
annotate the constructor with the `...@inject` annotation. This constructor  
should accept class dependencies as parameters. Most constructors will then  
assign the parameters to final fields.
+{{{
+public class RealBillingService implements BillingService {
+  private final CreditCardProcessor processorProvider;
+  private final TransactionLog transactionLogProvider;
+
+  @Inject
+  public RealBillingService(CreditCardProcessor processorProvider,
+      TransactionLog transactionLogProvider) {
+    this.processorProvider = processorProvider;
+    this.transactionLogProvider = transactionLogProvider;
+  }
+}}}
+If your class has no `...@inject`-annotated constructor, Guice will use a  
public, no-arguments constructor if it exists. Prefer the annotation, which  
documents that the type participates in dependency injection.
+
+Constructor injection works nicely with unit testing. If your class  
accepts all of its dependencies in a single constructor, you won't  
accidentally forget to set a dependency. When a new dependency is  
introduced, all of the calling code conveniently breaks! Fix the compile  
errors and you can be confident that everything is properly wired up.
+
+
+==Method Injection==
+Guice can inject methods that have the `...@inject` annotation. Dependencies  
take the form of parameters, which the injector resolves before invoking  
the method. Injected methods may have any number of parameters, and the  
method name does not impact injection.
+{{{
+public class PayPalCreditCardProcessor implements CreditCardProcessor {
+
+  private static final String DEFAULT_API_KEY = "development-use-only";
+
+  private String apiKey = DEFAULT_API_KEY;
+
+  @Inject
+  public void setApiKey(@Named("PayPal API key") String apiKey) {
+    this.apiKey = apiKey;
+  }
+}}}
+
+
+==Field Injection==
+Guice injects fields with the `...@inject` annotation. This is the most  
concise injection, but the least testable.
+{{{
+public class DatabaseTransactionLogProvider implements  
Provider<TransactionLog> {
+  @Inject Connection connection;
+
+  public TransactionLog get() {
+    return new DatabaseTransactionLog(connection);
+  }
+}
+}}}
+Avoid using field injection with `final` fields, which has  
[http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html#set(java.lang.Object,%20java.lang.Object)
  
weak semantics].
+
+
+==Optional Injections==
+Occasionally it's convenient to use a dependency when it exists and to  
fall back to a default when it doesn't. Method and field injections may be  
optional, which causes Guice to silently ignore them when the dependencies  
aren't available. To use optional injection, apply the  
`...@inject(optional=true)` annotation:
+{{{
+public class PayPalCreditCardProcessor implements CreditCardProcessor {
+  private static final String SANDBOX_API_KEY = "development-use-only";
+
+  private String apiKey = SANDBOX_API_KEY;
+
+  @Inject(optional=true)
+  public void setApiKey(@Named("PayPal API key") String apiKey) {
+    this.apiKey = apiKey;
+  }
+}}}
+Mixing optional injection and just-in-time bindings may yield surprising  
results. For example, the following field is always injected even when  
`Date` is not explicitly bound. This is because `Date` has a public  
no-arguments constructor that is eligible for just-in-time bindings.
+{{{
+  @Inject(optional=true) Date launchDate;
+}}}
+
+
+==Static Injections==
+TODO
+
+
+==requestInjection==
+Method and field injection can be used to initialize an existing instance.  
You can use the `Injector.injectMembers` API:
+{{{
+  public static void main(String[] args) {
+    Injector injector = Guice.createInjector(...);
+
+    CreditCardProcessor creditCardProcessor = new  
PayPalCreditCardProcessor();
+    injector.injectMembers(creditCardProcessor);
+}}}
+
+
+
+==Automatic Injection==
+TODO
+
+==A Note on Annotations==
+TODO
\ 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to