Skip to content

InstanceBindings

Googler edited this page Aug 6, 2020 · 7 revisions

Instance Bindings

You can bind a type to a specific instance of that type. This is usually only useful for objects that don't have dependencies of their own, such as value objects:

    bind(String.class)
        .annotatedWith(Names.named("JDBC URL"))
        .toInstance("jdbc:mysql://localhost/pizza");
    bind(Integer.class)
        .annotatedWith(Names.named("login timeout seconds"))
        .toInstance(10);

Avoid using .toInstance with objects that are complicated to create, since it can slow down application startup. You can use an @Provides method instead.

You can also bind constants using bindConstant:

  bindConstant()
      .annotatedWith(HttpPort.class)
      .to(8080);

bindConstant is a shortcut to bind primitive types and other constant types like String, enum and Class.

Clone this wiki locally