如何在 Hilt 中使用 @Binds 来指定要注入的接口所对应的组件?

huangapple go评论74阅读模式
英文:

How to specify component for injecting an interface using @Binds in hilt?

问题

以下是翻译的内容:

如果我们试图注入以下类,

class NoComponentRequiredClass @Inject constructor(){}

我们可以在不指定任何组件的情况下进行注入。

然而,如果我们试图提供一个依赖项作为接口的实现,我们必须创建一个模块,并随后必须指定我们要安装的组件。

例如:

interface Test{ 
  fun test() 
}

class TestImpl : Test{
    override fun test(){}
}

@Module
object Module{
  @Binds
  fun providesTest(test:TestImpl) : Test
}

除此之外,我还必须指定我们要进行注入的组件。

我希望能够在不指定组件的情况下进行Test的注入(因为NoComponentRequiredClass可以在不指定组件的情况下进行注入)。

如何在不指定组件的情况下注入Test,或者NoComponentRequiredClass是在哪个组件中注入的?

英文:

If we are trying to inject the following class,

class NoComponentRequiredClass @Inject constructor(){}

we can inject it without specifying any component for it to be installed in.

However, if we are trying to provide a dependency as an implementation of an interface, we have to create a Module and subsequently have to specify the component we are trying to installIn.

For example:

interface Test{ 
  fun test() 
}

class TestImpl : Test{
    overide fun test(){}
}

@Module
object Module{
  @Binds
  fun providesTest(test:TestImpl) : Test
}

Along with it I have to specify the component we are injecting it on.

I was hoping to inject it without specifying the component (As NoComponentRequiredClass can be injected without specifying component).

How to inject Test without specifying component or in which component is NoComponentRequiredClass injected in?

答案1

得分: -1

在使用 Hilt 时,当使用 @Binds 来为接口提供实现时,您需要定义声明绑定的模块,并指定安装该模块的组件。这是因为 Hilt 需要知道将该绑定关联到哪个组件。

如果您想要注入 Test 而不指定组件,可以使用 @Singleton 作用域。以下是示例:

@Singleton
class TestImpl @Inject constructor() : Test {
    override fun test() {}
}

@Module
@InstallIn(SingletonComponent::class) // 指定组件为 SingletonComponent
interface TestModule {
    @Binds
    fun bindTest(impl: TestImpl): Test
}

TestImpl 类被标注为 @Singleton,这意味着它将作为单例提供。TestModule 安装在 SingletonComponent 中,SingletonComponent 是 Hilt 中预定义的组件。这允许您注入 Test 接口而不需要显式指定组件。

英文:

In Hilt, when using @Binds to provide an implementation for an interface, you need to define the module in which the binding is declared and specify the component where that module is installed. This is because Hilt needs to know which component to associate with the binding.

If you want to inject Test without specifying a component, you can use the @Singleton scope.
Following is example,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

@Singleton
class TestImpl @Inject constructor() : Test {
    override fun test() {}
}

@Module
@InstallIn(SingletonComponent::class) // Specify the component as SingletonComponent
interface TestModule {
    @Binds
    fun bindTest(impl: TestImpl): Test
}

<!-- end snippet -->

the TestImpl class is annotated with @Singleton, which means it will be provided as a singleton instance. The TestModule is installed in the SingletonComponent, which is a predefined component in Hilt. This allows you to inject the Test interface without explicitly specifying a component.

huangapple
  • 本文由 发表于 2023年6月6日 08:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定