Bindings

Instance Bindings

qianmoQqianmoQ· 更新于 2026-09-23· 阅读 2 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

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.

评论

登录后参与评论

正在加载评论…