kotlin 中的 Dagger 2 静态提供程序方法 [英] Dagger 2 static provider methods in kotlin

查看:30
本文介绍了kotlin 中的 Dagger 2 静态提供程序方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近版本的 dagger 2 中所做的改进之一是具有静态提供方法的可能性.就这样:

With the recent versions of dagger 2 one of the improvements made are the possibility of having static provide methods. Simply so:

@Provides
static A providesA() {
  return A();
}

我想知道如何在 kotlin 中做到这一点?我试过了

I was wondering how does one go about doing this in kotlin? I've tried

@Module
class AModule {
  companion object {
    @JvmStatic
    @Provides
    fun providesA(): A = A()
  }
}

但我收到错误消息:

@Provides methods can only be present within a @Module or @ProducerModule

我猜这里的伴生对象有问题,但是我对 Kotlin 很陌生,我不确定如何做到这一点.甚至有可能吗?

I'm guessing there's something going on here with the companion object, however I'm quite new to Kotlin and I'm unsure of how one can do this. Is it even possible?

谢谢!

推荐答案

我现在无法测试,但我认为这应该可行:

I can't test it right now, but I think this should work:

@Module
object AModule {
    @JvmStatic
    @Provides
    fun providesA(): A = A()
}

这篇关于kotlin 中的 Dagger 2 静态提供程序方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆