Instance Bindings

通常,一个类没有其它依赖时,可以给它set一个它自己类型的实例对象。如:

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替代。

也可以使用bindConstant绑定常量:

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

bindConstant是绑定基本数据类型(primitive)和其他常量类型(如String、enum和Class)的快捷方式。

Copyright © qgao 2021-* all right reserved,powered by Gitbook该文件修订时间: 2022-05-30 14:53:39

results matching ""

    No results matching ""