Guice:当我在构造函数上有过多的 @Inject 时会发生什么

发布于 2025-01-13 03:50:45 字数 1243 浏览 4 评论 0原文

我有一个类 SomeClass 和一个 SomeModule 来向 Guice 注册它。

我发现构造函数 SomeClass 仅由 SomeModule 调用,并且 SomeModule 是唯一绑定 SomeClass 的地方代码> 发生。

这意味着 SomeClass 的构造函数上的 @Inject 是不需要的,因为 prop1prop2 被注入到 < code>SomeModule 并传递给构造函数。测试似乎也证明了我的发现。

我的问题是,当 Guice 看到这样的 @Inject 时会做什么?
另外如果我的@Inject过多会有什么副作用?

public static class SomeModule extends PrivateModule {

    @Provides
    @Singleton
    @Exposed
    private SomeClass someClass( SomeObject prop1, String prop2) {
        return new SomeClass(prop1, prop2);
    }
}


public class SomeClass {

    @Inject // unnecessary
    public SomeClass(SomeObject prop1, String prop2){
        ...
    }
}

如果我的理解是正确的,当你想将 Guice 管理的对象注入到构造函数的参数中时,你可以使用 @Inject 构造函数。
例如,如果我有 bind(SomeClass.class).in(Singleton.class) 以及 prop1prop2 的绑定,则 < code>@Inject SomeClass 构造函数,以便将 prop1prop2 注入到构造函数中。
但由于情况并非如此,这使得这里的 @Inject 不必要了

干杯

I have a class SomeClass and a SomeModule to register it with Guice.

I found out that the constructor SomeClass is only called by SomeModule, and that SomeModule is the only place where binding for SomeClass happens.

This means that the @Inject on SomeClass's constructor is not needed since prop1 and prop2 are injected inside SomeModule and passed to the constructor. And the testing also seems to prove my findings.

My question is that what Guice will do when it sees a @Inject like this?
Also what side effect will there be if I have an excessive @Inject?

public static class SomeModule extends PrivateModule {

    @Provides
    @Singleton
    @Exposed
    private SomeClass someClass( SomeObject prop1, String prop2) {
        return new SomeClass(prop1, prop2);
    }
}


public class SomeClass {

    @Inject // unnecessary
    public SomeClass(SomeObject prop1, String prop2){
        ...
    }
}

If my understanding is correct, you @Inject a constructor when you want to inject objects managed by Guice into the parameters of the constructor.
e.g. if I have bind(SomeClass.class).in(Singleton.class) and bindings for prop1 and prop2, it makes sense to @Inject SomeClass constructor in order to inject prop1 and prop2 into the constructor.
But since this isn't the case, this makes the @Inject here unnecessary

Cheers

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

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

发布评论

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

评论(1

颜漓半夏 2025-01-20 03:50:45

引用@Deepak的评论作为答案:

如果您在配置方法中定义绑定而不是为此类创建提供程序,则需要 @Inject。当您在configure 方法中定义绑定时,您就是在告诉Guice 为您实例化该对象。也就是说,它会寻找带有 @Inject 的构造函数来确定它必须注入哪些依赖项才能构造该对象。对于 Provider,您可以通过将该类所需的所有依赖项作为参数传递来自行创建对象。因此,@Inject 在这种情况下没有任何意义

Quoting @Deepak's comment as the answer:

You need @Inject if you were defining a binding in the configure method instead of creating a Provider for this class. When you define a binding in the configure method, you are telling Guice to instantiate the object for you. That is when it looks for a constructor with @Inject to figure out what dependencies it has to Inject to construct that object. In case of Provider, you are creating the object yourself by passing all the dependencies required for that class as arguments. So, @Inject has no meaning at that case

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