import static com.google.inject.Names.named; public class MyModule extends AbstractModule { protected void configure() { bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON); bind(BarImpl.class); link(Bar.class).to(BarImpl.class); bindConstant(named("port")).to(8080); } } @author crazybob@google.com (Bob Lee)]]> bindings) which will be used to create an {@link Injector}. Guice provides this object to your application's {@link Module}s so they may each contribute their own bindings.

The bindings contributed by {@code Module}s define how the {@code Injector} resolves dependencies. A {@link Key} consisting of a type and optional annotation uniquely identifies a binding within an {@code Injector}.

You may bind from a key to:

  • Another binding, which this binding's key is now "aliased to"
  • Another binding, which references a {@link Provider} for this key
  • A preconstructed instance
  • A preconstructed instance which should be used as the {@link Provider} for this binding

In addition, a binding may have an associated scope, such as {@link Scopes#SINGLETON}, and singleton bindings may specify eager or lazy initialization.

See the users' guide appendix, "How the Injector resolves injection requests," to better understand binding resolution.

After an {@code Injector} has been created, its bindings may be examined using methods like {@link Injector#getBinding(Key)}, but this read-only {@link Binding} type is not used when creating the bindings.]]> {@code @}Retention(RUNTIME) {@code @}Target({ FIELD, PARAMETER }) {@code @}BindingAnnotation public {@code @}interface Transactional {} @author crazybob@google.com (Bob Lee)]]>

  • Every instance it constructs. The class being constructed must have exactly one of its constructors marked with {@code @Inject} or must have a constructor taking no parameters. The Injector then proceeds to perform method and field injections.
  • Pre-constructed instances passed to {@link Injector#injectMembers}, {@link com.google.inject.binder.LinkedBindingBuilder#toInstance(Object)} and {@link com.google.inject.binder.LinkedBindingBuilder#toProvider(Provider)}. In this case all constructors are, of course, ignored.
  • Static fields and methods of classes which any {@link Module} has specifically requested static injection for, using {@link Binder#requestStaticInjection}. In all cases, a member can be injected regardless of its Java access specifier (private, default, protected, public). @author crazybob@google.com (Bob Lee)]]> The {@code Injector} API has a few additional features: it allows pre-constructed instances to have their fields and methods injected and offers programmatic introspection to support tool development.

    Contains several default bindings:

    • This {@link Injector} instance itself
    • A {@code Provider} for each binding of type {@code T}
    • The {@link java.util.logging.Logger} for the class being injected
    • The {@link Stage} in which the Injector was created
    Injectors are created using the facade class {@link Guice}. @author crazybob@google.com (Bob Lee)]]>
    Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.

    Example usage for a binding of type {@code Foo} annotated with {@code @Bar}:

    {@code new Key(Bar.class) {}}.]]> Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.

    Example usage for a binding of type {@code Foo} annotated with {@code @Bar}:

    {@code new Key(new Bar()) {}}.]]> Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.

    Example usage for a binding of type {@code Foo}:

    {@code new Key() {}}.]]> For example, {@code Key.get(Service.class, Transactional.class)} will match:

       {@literal @}Inject
       public void setService({@literal @}Transactional Service service) {
         ...
       }
     

    {@code Key} supports generic types via subclassing just like {@link TypeLiteral}. @author crazybob@google.com (Bob Lee)]]> Your Module classes can use a more streamlined syntax by extending {@link AbstractModule} rather than implementing this interface directly.]]>

  • When the default means for obtaining instances (an injectable or parameterless constructor) is insufficient for a particular binding, the module can specify a custom {@code Provider} instead, to control exactly how Guice creates or obtains instances for the binding.
  • An implementation class may always choose to have a {@code Provider} instance injected, rather than having a {@code T} injected directly. This may give you access to multiple instances, instances you wish to safely mutate and discard, instances which are out of scope (e.g. using a {@code @RequestScoped} object from within a {@code @SessionScoped} object), or instances you don't want to initialize until they are absolutely needed.
  • A custom {@link Scope} is implemented as a decorator of {@code Provider}, which decides when to delegate to the backing provider and when to provide the instance some other way.
  • The {@link Injector} offers access to the {@code Provider} it uses to fulfill requests for a given key, via the {@link Injector#getProvider} methods. @param the type of object this provider provides @author crazybob@google.com (Bob Lee)]]> Scope implementations are strongly encouraged to override {@link Object#toString} in the returned provider and include the backing provider's {@code toString()} output. @param key binding key @param unscoped locates an instance when one doesn't already exist in this scope. @return a new provider which only delegates to the given unscoped provider when an instance of the requested object doesn't already exist in this scope]]> no scope, meaning it has no state from the framework's perspective -- the {@code Injector} creates it, injects it once into the class that required it, and then immediately forgets it. Associating a scope with a particular binding allows the created instance to be "remembered" and possibly used again for other injections. @see Scopes#SINGLETON @author crazybob@google.com (Bob Lee)]]> {@code @}Retention(RUNTIME) {@code @}Target(TYPE) {@code @}ScopeAnnotation public {@code @}interface SessionScoped {} @author crazybob@google.com (Bob Lee)]]> Clients create an empty anonymous subclass. Doing so embeds the type parameter in the anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.]]> For example, to create a type literal for {@code List}, you can create an empty anonymous inner class:

    {@code TypeLiteral> list = new TypeLiteral>() {};}

    Assumes that type {@code T} implements {@link Object#equals} and {@link Object#hashCode()} as value (as opposed to identity) comparison. @author crazybob@google.com (Bob Lee)]]> bind(DataSource.class).toProvider(fromJndi(DataSource.class, "java:...")); ]]> } when you want the HTTP request parameter map to be injected. @author crazybob@google.com (Bob Lee)]]> Skipping only takes place after this method is called.]]> bind(DataSource.class) .toProvider(fromSpring(DataSource.class, "dataSource")); ]]>