Guice:Binder#bindConstant()和Binder#bind()... toInstance之间的区别 [英] Guice: Difference between Binder#bindConstant() and Binder#bind() ... toInstance

查看:229
本文介绍了Guice:Binder#bindConstant()和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 and Strings, 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 an int to Integer.class rather than Integer.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆