在不绑定 Guice 的情况下
Bind in Guice without to
我有一个问题:
通常在 Guice 中我使用 bind(classe).to(another_class_Implementation) ...
但是我在代码源中发现他们只使用了 bind(class) (没有部分 ".to(another_class_Implementation)" )...
这是什么意思(没有 "to or as" 的 bind(class) )?
这是有问题的部分代码:
public class RestModule extends AbstractModule {
@Override
protected void configure() {
bind(RootResource.class);
bind(DeveloperUtilsRedirector.class);
bind(M34Repository.class).to(M34RepositoryImpl.class);
bind(IGARepository.class).to(IGARepositoryImpl.class);
感谢回答
没有 to
的 bind
语句在 Guice 文档中被称为 Untargeted Binding(在 wiki URL 中被错误拼写为“Untargetted Bindings”)。来自该维基页面:
You may create bindings without specifying a target. This is most useful for concrete classes and types annotated by either @ImplementedBy
or @ProvidedBy
. An untargetted [sic] binding informs the injector about a type, so it may prepare dependencies eagerly.
您会在 Guice 中看到它的三个用途:
通过预先加载略微提高性能。
当 Guice 遇到它没有绑定的依赖项时(例如,class A),它会检查 class 以查看是否可以通过 @Inject
-注释或零参数 public 构造函数。如果是这样,Guice 会创建一个 Just-In-Time binding(或“隐式绑定”)。这是通过反射完成的,并且可能会导致其他绑定的级联(请求 A 检查 A,然后是 A 的依赖 B,然后是 B 的依赖 C,等等),这可能会导致运行时速度变慢。
通过先发制人地进行非目标绑定,您可以将可注入的信息告知 Guice class,这允许它在启动时支付反射成本以获得更可预测的性能。
如果 Guice 无法创建您注入的对象,它会抛出异常,但在 @ImplementedBy 或 @ProvidedBy(或 getInstance
或 injectMembers
)的情况下,Guice 会如果它还没有检查缺少绑定的 class,则不会失败。通过列出您使用的 classes,Guice 将像 (1) 中那样预分析这些对象,但也会 在应用程序启动时识别缺少绑定 。这在开发过程中可能很方便,特别是如果您在应用程序启动后很长时间内使用 getInstance
或 injectMembers
注入对象;您可能更希望失败立即发生。
虽然默认情况下启用隐式绑定,但可以通过 requireExplicitBindings
禁用它们。这意味着任何注入的 classes 都需要关联绑定,包括具有合格构造函数的 classes。无目标绑定可以轻松解决这种情况。
我有一个问题: 通常在 Guice 中我使用 bind(classe).to(another_class_Implementation) ...
但是我在代码源中发现他们只使用了 bind(class) (没有部分 ".to(another_class_Implementation)" )...
这是什么意思(没有 "to or as" 的 bind(class) )?
这是有问题的部分代码:
public class RestModule extends AbstractModule {
@Override
protected void configure() {
bind(RootResource.class);
bind(DeveloperUtilsRedirector.class);
bind(M34Repository.class).to(M34RepositoryImpl.class);
bind(IGARepository.class).to(IGARepositoryImpl.class);
感谢回答
没有 to
的 bind
语句在 Guice 文档中被称为 Untargeted Binding(在 wiki URL 中被错误拼写为“Untargetted Bindings”)。来自该维基页面:
You may create bindings without specifying a target. This is most useful for concrete classes and types annotated by either
@ImplementedBy
or@ProvidedBy
. An untargetted [sic] binding informs the injector about a type, so it may prepare dependencies eagerly.
您会在 Guice 中看到它的三个用途:
通过预先加载略微提高性能。
当 Guice 遇到它没有绑定的依赖项时(例如,class A),它会检查 class 以查看是否可以通过
@Inject
-注释或零参数 public 构造函数。如果是这样,Guice 会创建一个 Just-In-Time binding(或“隐式绑定”)。这是通过反射完成的,并且可能会导致其他绑定的级联(请求 A 检查 A,然后是 A 的依赖 B,然后是 B 的依赖 C,等等),这可能会导致运行时速度变慢。通过先发制人地进行非目标绑定,您可以将可注入的信息告知 Guice class,这允许它在启动时支付反射成本以获得更可预测的性能。
如果 Guice 无法创建您注入的对象,它会抛出异常,但在 @ImplementedBy 或 @ProvidedBy(或
getInstance
或injectMembers
)的情况下,Guice 会如果它还没有检查缺少绑定的 class,则不会失败。通过列出您使用的 classes,Guice 将像 (1) 中那样预分析这些对象,但也会 在应用程序启动时识别缺少绑定 。这在开发过程中可能很方便,特别是如果您在应用程序启动后很长时间内使用getInstance
或injectMembers
注入对象;您可能更希望失败立即发生。虽然默认情况下启用隐式绑定,但可以通过
requireExplicitBindings
禁用它们。这意味着任何注入的 classes 都需要关联绑定,包括具有合格构造函数的 classes。无目标绑定可以轻松解决这种情况。