简体   繁体   English

匕首2,@ set:Inject和@Inject有什么区别

[英]dagger 2, what is difference between @set:Inject and @Inject

In a android, kotlin project, seeing this @set:Inject but could not find a good explanation. 在一个Android Kotlin项目中,看到此@set:Inject但找不到很好的解释。 Anyone knows? 有谁知道?

object Controller {

    @set:Inject
    lateinit var someData: SomeData

The @Inject annotation can be used for method, constructor, or field: @Inject批注可用于方法,构造函数或字段:

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

It is important to remember that Java code will be generated from this Kotlin code and for one statement in Kotlin you can have multiple Java elements and that is why @set:Inject explicitly specifies that the @Inject annotation should be applied to the setter that will be generated in Java. 重要的是要记住,Java代码将从此Kotlin代码生成,并且对于Kotlin中的一条语句,您可以具有多个Java元素,这就是@set:Inject明确指定将@Inject注释应用于将要使用的setter的原因。用Java生成。

What happens if there is no use-site target defined? 如果未定义使用场所目标,该怎么办? Official documentation provides a good explanation: 官方文档提供了很好的解释:

If you don't specify a use-site target, the target is chosen according to the @Target annotation of the annotation being used. 如果未指定使用场所目标,则根据所使用注释的@Target注释选择目标。 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); 属性(此目标的注释对Java不可见);
  • field; 领域;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM