在一个 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/