android - Dagger 2,@set :Inject and @Inject有什么区别

标签 android dependency-injection kotlin dagger-2

在一个 android, kotlin 项目中,看到这个 @set:Inject但找不到很好的解释。有谁知道?

object Controller {

    @set:Inject
    lateinit var someData: SomeData

最佳答案

@Inject注解可用于方法、构造函数或字段:

@Target(value={METHOD,CONSTRUCTOR,FIELD})

重要的是要记住,Java 代码将从此 Kotlin 代码生成,对于 Kotlin 中的一条语句,您可以有多个 Java 元素,这就是 @set:Inject 的原因。明确指定 @Inject注释应应用于将在 Java 中生成的 setter 。

如果没有定义使用站点目标会发生什么? Official documentation提供了一个很好的解释:

If you don't specify a use-site target, the target is chosen according to the @Target annotation of the annotation being used. If there are multiple applicable targets, the first applicable target from the following list is used:

  • param (constructor parameter);
  • property (annotations with this target are not visible to Java);
  • field;

关于android - Dagger 2,@set :Inject and @Inject有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51522423/

相关文章:

带有返回值的 Kotlin 协程

java - Android - EditText-可选择,不可编辑

android - android-sdk\tools 中缺少 Emulator.exe

java - 如何解析 Android XML 中的同名标签?

php - 无法将路由器服务传递给 symfony 2.6 中的 Twig 扩展

c# - MVVM 和 IOC : Handling View Model's Class Invariants

java - Android 时区默认返回错误值

Android如何查询android中巨大的数据库(光标大小限制为1MB)

angular - 如何将窗口注入(inject) Angular 2.1.0

android - 如何使用时间戳在Firebase中过滤结果?安卓系统