本文介绍了与Guice绑定而无需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:通常在Guice中,我使用bind(classe).to(another_class_Implementation)...

I have a question:Usually in Guice I use bind(classe).to(another_class_Implementation) ...

但是我在代码源中发现他们仅使用bind(class)(不带".to(another_class_Implementation)"部分)...

However I found in a code source that they have used bind(class) only ( without the part ".to(another_class_Implementation)" ) ...

这是什么意思(不带"to或as"的bind(class))?

What does this mean ( bind(class) without "to or as" ) ?

这是有问题的代码的一部分:

Here is the part of code in question:

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);

感谢答谢

推荐答案

不带tobind语句称为非目标绑定(在Wiki URL中拼写为非目标绑定").在该Wiki页面上:

A bind statement without a to is called an Untargeted Binding (misspelled as "Untargetted Bindings" in the wiki URL) in the Guice docs. From that wiki page:

您将在Guice中看到这三个目的:

You'll see this in Guice for three purposes:

  1. 通过积极的加载可以略微提高性能.

  1. Slight performance improvement via eager loading.

当Guice遇到一个没有绑定的依赖项(例如,类A)时,它将检查该类,以查看是否可以通过@Inject注释或零参数的公共构造函数将其注入.如果是这样,Guice将创建及时绑定(或隐式绑定" ).这是通过反射完成的,并且可能导致其他绑定的级联(请求A检查A,然后A的依赖项B,然后B的依赖项C,依此类推),这可能会导致运行时变慢.

When Guice encounters a dependency that it doesn't have a binding for (say, class A), it inspects the class to see if it can be injected via an @Inject-annotated or zero-arg public constructor. If so, Guice creates a Just-In-Time binding (or "implicit binding"). This is done through reflection, and may cause a cascade of other bindings (requesting A inspects A, then A's dependency B, then B's dependency C, and so forth), which can cause some runtime slowdown.

通过抢先建立无目标的绑定,您可以向Guice通知一个可注入的类,这使它可以在启动时支付反射成本,以获得更可预测的性能.

By pre-emptively making an untargeted binding, you inform Guice about an injectable class, which allow it to pay that reflection cost at startup for more predictable performance.

如果无法创建注入的对象,Guice将引发异常,但对于@ImplementedBy或@ProvidedBy(或getInstanceinjectMembers),Guice如果未检查,将不会失败缺少绑定的类.通过列出您使用的类,Guice将像(1)中一样预先分析那些对象,但还将在应用启动时识别出绑定丢失.这在开发过程中可能很方便,尤其是在应用程序启动后很长时间使用getInstanceinjectMembers注入对象时;您可能更希望这种失败立即发生.

Guice will throw an exception if it cannot create the object you inject, but in the case of @ImplementedBy or @ProvidedBy (or getInstance or injectMembers) Guice will not fail if it hasn't inspected the class with the missing binding yet. By listing classes you use, Guice will pre-analyze those objects as in (1), but will also recognize at app startup that a binding is missing. This may be handy during development, especially if you are injecting an object with getInstance or injectMembers long after application startup; you might prefer that failure to happen immediately.

尽管默认情况下会启用隐式绑定,但可以通过 requireExplicitBindings .这意味着任何注入的类都需要具有关联的绑定,包括具有合格构造函数的类.不受目标约束的绑定可以轻松解决这种情况.

Though implicit bindings are enabled by default, they can be disabled through requireExplicitBindings. This means that any injected classes need to have associated bindings, including classes with eligible constructors. An untargeted binding would resolve that situation easily.

这篇关于与Guice绑定而无需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:06