何时提供显式绑定以及何时在Guice上利用自动绑定 [英] When to provide explicit binding and when to leverage the automatic binding on Guice

查看:55
本文介绍了何时提供显式绑定以及何时在Guice上利用自动绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我纳闷,为什么我们有时需要在模块中而不是其他时候显式提供绑定?
Guice如何决定何时需要绑定以及何时不需要绑定?

I was wonder, why do we need to provide bindings explicitly sometimes and not other times in the module? How does Guice decide when we need the binding and when not?

是因为如果一个依赖项没有多个实现,并且将其作为依赖项注入到其他地方,它会被自动绑定为依赖图的一部分吗?

Is it because if a dependency has no multiple implementations and it is injected elsewhere as a dependencies it will be automatically bound as part of a dependency graph?

谢谢

推荐答案

您需要绑定(通过 bind @Provides 方法或 @ImplementedBy 注释)对于您想要注入的,不是带有无参数构造函数或带有 @Inject 带注释的构造函数的具体类的任何东西。

You need a binding (either through a bind, an @Provides method or an @ImplementedBy annotation) for anything you want injected that is not a concrete class with a no-arg constructor or an @Inject annotated constructor.

这篇关于何时提供显式绑定以及何时在Guice上利用自动绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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