Guice:Binder#bindConstant()和Binder#bind()... toInstance之间的区别 [英] Guice: Difference between Binder#bindConstant() and Binder#bind() ... toInstance
问题描述
我想问一下
bindConstant().annotatedWith(Names.named("keepAliveInterval")).to(60);
和
bind(Integer.TYPE).annotatedWith(Names.named("keepAliveInterval")).toInstance(60);
我想用Names.bindProperties(binder(),prop)加载我的所有配置属性;在我的模块中,我发现它使用后者来绑定属性。
I would like to load all my configuration properties with Names.bindProperties(binder(), prop); in my module and I discovered that it uses the latter one for binding properties.
谢谢,问候
Marek
推荐答案
我认为使用 bindConstant()
的理由是:
- 它要求您使用带注释的绑定。你不能
bindConstant()。到(foo)
。由于与它绑定的类型是基元和String
s,因此无注释绑定对它们中的任何一个都没有意义。 - 由于您不必指定类型(顺便说一下,
bindConstant()
绑定int
到Integer.class
而不是Integer.TYPE
,不确定是否重要。)
- It requires that you use an annotated binding. You can't do
bindConstant().to(foo)
. Since the types you bind with it are primitives andString
s, it's unlikely that an annotation-less binding would make sense for any of them. - It requires less effort since you don't have to specify the type (by the way,
bindConstant()
binds anint
toInteger.class
rather thanInteger.TYPE
, not sure if that matters).
我认为 Names.bindProperties
不使用 bindConstant
只是因为它的内部代码和一些代码可以在进行绑定的过程中跳过一两步。在你自己的模块中,我只使用 bindConstant
,因为它很简单,更清晰。
I think Names.bindProperties
doesn't use bindConstant
just because it's internal code and a little more code is OK to skip a step or two in the process of making a binding. In your own modules, I'd just use bindConstant
because it's easy and more clear.
这篇关于Guice:Binder#bindConstant()和Binder#bind()... toInstance之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!