Author: limpbizkit
Date: Thu Jan  1 17:52:16 2009
New Revision: 781

Added:
    wiki/FrequentlyAskedQuestions.wiki

Log:
Created wiki page through web user interface.

Added: wiki/FrequentlyAskedQuestions.wiki
==============================================================================
--- (empty file)
+++ wiki/FrequentlyAskedQuestions.wiki  Thu Jan  1 17:52:16 2009
@@ -0,0 +1,135 @@
+=Frequently Asked Questions=
+
+===How do I inject configuration parameters?===
+You need a binding annotation to identify your parameter. Create an  
annotation class that defines the parameter:
+{{{
+/**
+ * Annotates the URL of the foo server.
+ */
+...@retention(RetentionPolicy.RUNTIME)
+...@target({ElementType.FIELD, ElementType.PARAMETER})
+...@bindingannotation
+public @interface FooServerAddress {}
+}}}
+Bind the annotation to its value value in your module:
+{{{
+public class FooModule {
+  private final String fooServerAddress;
+
+  /**
+   * @param fooServerAddress the URL of the foo server.
+   */
+  public FooModule(String fooServerAddress) {
+    this.fooServerAddress = fooServerAddress;
+  }
+
+  @Override public void configure() {
+     
bindConstant().annotatedWith(FooServerAddress.class).to(fooServerAddress);
+    ...
+  }
+}
+}}}
+Finally, inject it into your class:
+{{{
+public class FooClient {
+
+  @Inject
+  FooClient(@FooServerAddress String fooServerAddress) {
+    ...
+  }
+}}}
+You may save some keystrokes by using Guice's built-in `...@named` binding  
annotation rather than creating your own.
+
+===How do I load configuration properties?===
+Use  
[http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/name/Names.html#bindProperties(com.google.inject.Binder,%20java.util.Properties)
  
Names.bindProperties()] to create bindings for each of the properties in a  
configuration file.
+
+===How do I pass a parameter when creating an object via Guice?===
+You can't directly pass a parameter into an injected value. But you can  
use Guice to create a `Factory`, and use that factory to create your object.
+{{{
+public class Thing {
+  // note: no @Inject annotation here
+  private Thing(A a, B b) {
+    ...
+  }
+
+  public static class Factory {
+    @Inject
+    public Factory(A a) { ... }
+    public Thing make(B b) { ... }
+  }
+}
+}}}
+
+{{{
+public class Example {
+  @Inject
+  public Example(Thing.Factory factory) { ... }
+}
+}}}
+See [AssistedInject], which can be used to remove the factory boilerplate.
+
+===How do I build two similar but slightly different trees of objects?===
+This is commonly called the "robot legs" problem: How to create a robot  
with a two `Leg` objects, the left one injected with a `LeftFoot`, and the  
right one with a `RightFoot`. But only one `Leg` class that's reused in  
both contexts.
+
+There's a [http://docs.google.com/Doc?id=dhfm3hw2_51d2tmv6pc  
PrivateModules solution]. It uses two separate private modules, a `...@left`  
one and an `...@right` one. Each has a binding for the unannotated  
`Foot.class` and `Leg.class`, and exposes a binding for the annotated  
`Leg.class`:
+{{{
+class LegModule extends PrivateModule {
+  private final Class<? extends Annotation> annotation;
+
+  LegModule(Class<? extends Annotation> annotation) {
+    this.annotation = annotation;
+  }
+
+  @Override protected void configurePrivateBindings() {
+    bind(Leg.class).annotatedWith(annotation).to(Leg.class);
+    expose(Leg.class).annotatedWith(annotation);
+
+    bindFoot();
+  }
+
+  abstract void bindFoot();
+}
+}}}
+{{{
+  public static void main(String[] args) {
+    Injector injector = Guice.createInjector(new LegModule(Left.class) {
+      @Override void bindFoot() {
+        bind(Foot.class).toInstance(new Foot("leftie"));
+      }
+    }, new LegModule(Right.class) {
+      @Override void bindFoot() {
+        bind(Foot.class).toInstance(new Foot("righty"));
+      }
+    });
+  }
+}}}
+
+===How can I inject an inner class?===
+Guice doesn't support this.  However, you can inject a _nested_ class  
(sometimes called a "static inner class"):
+{{{
+class Outer {
+  static class Nested {
+    ...
+  }
+}
+}}}
+
+=== How to inject class with generic type? ===
+You may need to inject a class with a parameterized type, like  
`List<String>`:
+{{{
+class Example {
+  @Inject
+  void setList(List<String> list) {
+    ...
+  }
+}
+}}}
+You need to use  
[http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/TypeLiteral.html
  
TypeLiteral] to create the binding. `TypeLiteral` is a special class that  
allows you to specify a full parameterized type.
+{{{
+  @Override public void configure() {
+    bind(new TypeLiteral<List<String>>() {}).toInstance(new  
ArrayList<String>());
+  }
+}}}
+
+===How can I get other questions answered?===
+Please post to the [http://groups.google.com/group/google-guice  
google-guice] discussion group.
\ 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