Locked History Actions

guice/Manual/UserGuide/Bindings/InstanceBindings

インスタンスバインディング

あるタイプに対し、そのタイプの特定のインスタンスをバインドすることができる。 これは通常、バリューオブジェクトのような、それ自体の依存性を持たないオブジェクトについて有効である。

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

複雑な生成を行うようなオブジェクトについて.toInstanceを使うのは避けた方がよい。 アプリのスタートアップを遅くするからだ。その代わりに@Providesメソッドを使った方がよい。