Guice 如何在没有 @Inject 注释的构造函数的情况下注入类?

发布于 2025-01-09 04:53:09 字数 682 浏览 1 评论 0原文

我试图了解 DI 在我们的代码库 (Kotlin) 中的使用方式。我们使用 google guice 进行依赖注入。

这是一个示例类:

class ServiceClass @Inject construct(
     private val depA: DepA,
     private val depB: DepB
) { ........ }

在模块类中:

@Provides
fun provideDepA(): DepA {
 //constructs DepA object and returns it
}

DepB 类:

class DepB { .... }

据我所知,对于用 @Inject 注释的变量,Google Guice 将使用模块类来解决这些依赖关系。因此,DepA 对象的注入方式是有意义的。

但是 DepB 呢?我们如何能够在不指定任何地方的情况下注入 DepB?

I'm trying to understand how DI is used in our codebase (Kotlin). We are using google guice for dependency injection.

Here is a sample class:

class ServiceClass @Inject construct(
     private val depA: DepA,
     private val depB: DepB
) { ........ }

In Module class:

@Provides
fun provideDepA(): DepA {
 //constructs DepA object and returns it
}

DepB class:

class DepB { .... }

As far as I know for the variables annotated with @Inject, Google Guice will look to resolve those dependencies using module class. So it makes sense how DepA object is injected.

But what about DepB? How are we able to inject DepB without specifying it anywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2025-01-16 04:53:09

未在模块中声明的 Guice 绑定称为即时绑定,默认情况下,Guice 愿意调用带有零参数的构造函数:

Guice 维基:JustInTimeBindings

Guice 可以通过使用类型的可注入构造函数来创建具体类型的绑定。 Guice 认为构造函数可注入,如果:

  • (推荐) 构造函数使用 @Inject 显式注释(com.google.inject.Injectjavax.inject.Inject支持)。
  • 或者,构造函数采用零个参数,
    • 构造函数是非私有的,并且在非私有类中定义(Guice 仅当在私有类中定义时才支持私有构造函数,但是,不建议使用私有构造函数,因为由于成本原因,它们在 Guice 中可能会很慢反思)。
    • 注入器未选择要求显式 @Inject 构造函数,请参阅下面的显式 @Inject 构造函数部分。

如果您没有所需的显式绑定,并且您有公共无参数构造函数,Guice 将调用它,就像您用 @Inject 标记它一样。这与 Dagger 形成鲜明对比,Dagger 调用 @Inject-注释的构造函数

Dagger 开发指南

如果您的类具有 @Inject 注释字段,但没有 @Inject 注释构造函数,则 Dagger 将根据请求注入这些字段,但不会创建新实例。添加带有 @Inject 注释的无参构造函数,以指示 Dagger 也可以创建实例。

...

缺乏@Inject注解的类无法由Dagger构建。

在 Guice 和 Dagger 中,您可以使用 @Provides 方法自行构造对象,在 Guice 中,您还可以使用模块来 使用参数显式绑定到构造函数,但不使用 @Inject 构造函数。

Guice bindings that are not declared in modules are known as Just-in-time Bindings, and by default Guice is willing to call constructors that take zero arguments:

Guice Wiki: JustInTimeBindings

Guice can create bindings for concrete types by using the type's injectable constructor. Guice considers a constructor injectable if:

  • (recommended) The constructor is explicitly annotated with @Inject (both com.google.inject.Inject and javax.inject.Inject are supported).
  • or, the constructor takes zero arguments, and
    • the constructor is non-private and defined in a non-private class (Guice supports private constructor only when it is defined in a private class, however, private constructors are not recommended because they can be slow in Guice due to the cost of reflection).
    • the injector has not opted in to require explicit @Inject constructor, see explicit @Inject constructors section below.

If you haven't required explicit bindings and you have a public no-arg constructor, Guice will call it as if you had marked it with @Inject. This is in contrast to Dagger, which will only call @Inject-annotated constructors:

Dagger Dev Guide

If your class has @Inject-annotated fields but no @Inject-annotated constructor, Dagger will inject those fields if requested, but will not create new instances. Add a no-argument constructor with the @Inject annotation to indicate that Dagger may create instances as well.

...

Classes that lack @Inject annotations cannot be constructed by Dagger.

In both Guice and Dagger you can construct the objects yourself in @Provides methods, and in Guice you can also use a Module to explicitly bind to a constructor with arguments and without an @Inject constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文