What's new? | Help | Directory | Sign in
Google
google-guice
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 5 and above, brought to you by Google.
  
  
  
  
    
Search
for
Updated May 12 (4 days ago) by limpbizkit
Labels: Featured
Guice10Recipes  
How to do it with Guice 1.0

An incomplete FAQ explaining how to accomplish various tasks using Guice 1.0.

How do I inject configuration parameters into objects created via Guice?

The recommended way is write a custom annotation for each configuration parameter:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface Greeting {}
@Inject
Foo(@Greeting String greeting) { ... }
bindConstant().annotatedWith(Greeting.class).to("Hello!");

If you don't want to write an annotation for each parameter, you could use the @Named annotation like this:

@Inject
Foo(@Named("greeting") String greeting) { ... }
bindConstant().annotatedWith(Names.named("greeting")).to("Hello!");

It's less typing, but then you don't get a compiler error if you misspell the name in the annotation.

Another way would be to write a Config class with a getter for each parameter, but that makes it more difficult to tell exactly which parameters are used by each class.

How do I load configuration parameters from a file?

Guice 1.0 doesn't provide this, but you can write your own module that reads the file and binds the parameters.

The advantage of using annotations and a module is that it separates configuration loading from configuration injection, so you can change where configuration parameters come from without changing the classes that use them.

How do I pass a parameter when creating an object via Guice?

Guice doesn't have direct support for this in 1.0. You have to either write a factory class or use AssistedInject. When writing a factory, the factory should be created via Guice, but the object itself is created by the factory, not by Guice.

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) { ... }
}

(Tracked in issue 131)

How do I build two similar but slightly different trees of objects?

(Also known as the "robot legs" problem.)

Currently you need to use two separate injectors for this.

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 do I inject a null?

Guice 1.0 doesn't support this. Instead, you could use an optional dependency:

class Example {
  @Inject(optional=true)
  void setFoo(String value) { ... }
}

Then, if you don't bind the parameter at all, it won't be set.

Unfortunately this only works for types that can be used as constants (such as java.lang.String), not classes you define. (See issue 107.)

A future version of Guice may allow nulls. (See issue issue 112)

An alternative is to use a null object.

How to inject class with generic type?

If you need to inject class with generic type, like:

class Example {
  @Inject
  void setList(List<String> list) { ... }
}

You may wonder that you can use binding:

bind(List.class).toInstance(new ArrayList<String>());

In this case you will get following error: com.google.inject.CreationException?: Guice configuration errors: Binding to java.util.List<java.lang.String> not found. No bindings to that type were found.

To get it work you should use such binding:

bind(new TypeLiteral<List<String>>() {}).toInstance(new ArrayList<String>());

For more information read about TypeLiteral

How can I get other questions answered, or contribute a recipe?

If you have a recipe to contribute, please add it here as a comment and we will eventually add it to the recipe book.

For questions, please post to the google-guice discussion group.



Sign in to add a comment