gpt4 book ai didi

java - 是否可以将子组件依赖注入(inject)到父组件中?

转载 作者:行者123 更新时间:2023-12-02 10:32:56 25 4
gpt4 key购买 nike

假设我有以下对象图:

  • 名为 AppComponent 的组件范围为@AppScope (@Singleton 等效)
  • 名为 SessionComponent 的子组件范围为@SessionScope ,它是 AppComponent 的子组件

SessionComponent在每个 session 开始时都会创建(注入(inject))依赖项。

是否可以注入(inject)@SessionScope依赖项(在 SessionComponent 提供)到 @AppScope 中依赖项(在 AppComponent 处提供)?

反之亦然,如 Component 中所述。文档:

The simplest way to relate two components is by declaring a Subcomponent. A subcomponent behaves exactly like a component, but has its implementation generated within a parent component or subcomponent. That relationship allows the subcomponent implementation to inherit the entire binding graph from its parent when it is declared.

我发现可以通过编程方式使用 this approach ,但是是否可以仅使用 Dagger API 来完成?

最佳答案

不,你不能:

Bindings that are installed into a component can not see bindings from its subcomponent. Instead, it's the other way around: bindings in a subcomponent can depend on bindings in a parent component.

如本answer所述来自您的链接。

关于java - 是否可以将子组件依赖注入(inject)到父组件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53498763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com