Singleton
You can use scope annotations to limit the lifetime of an object to the lifetime of its component. This means that the same instance of a dependency is used every time that type needs to be provided.
有些时候我们希望只有一个单例,比如 retrofit,okHttpClient,或者是 xApi,就可以使用这个。
@Singleton
class Person @Inject constructor(private val tea: Tea) {
fun drink() {
Logger.d("drink $tea")
}
}
@Singleton
@Component(modules = [AModule::class])
interface AComponent {
fun iCanInject(a: MainActivity)
fun giveMeAPerson():Person
}
override fun onCreate(savedInstanceState: Bundle?) {
...
val component = DaggerAComponent.create()
Logger.d(component.giveMeAPerson())
Logger.d(component.giveMeAPerson())
}
2020-03-26 18:39:10.453 7194-7194/com.example.myapplication D/PRETTY_LOGGER: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2020-03-26 18:39:10.455 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ Thread: main
2020-03-26 18:39:10.455 7194-7194/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-26 18:39:10.456 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ Activity.performCreate (Activity.java:7990)
2020-03-26 18:39:10.456 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ MainActivity.onCreate (MainActivity.kt:19)
2020-03-26 18:39:10.457 7194-7194/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-26 18:39:10.457 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ com.example.myapplication.Person@7e7be6f
2020-03-26 18:39:10.457 7194-7194/com.example.myapplication D/PRETTY_LOGGER: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2020-03-26 18:39:10.458 7194-7194/com.example.myapplication D/PRETTY_LOGGER: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2020-03-26 18:39:10.458 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ Thread: main
2020-03-26 18:39:10.458 7194-7194/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-26 18:39:10.459 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ Activity.performCreate (Activity.java:7990)
2020-03-26 18:39:10.460 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ MainActivity.onCreate (MainActivity.kt:20)
2020-03-26 18:39:10.460 7194-7194/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-26 18:39:10.460 7194-7194/com.example.myapplication D/PRETTY_LOGGER: │ com.example.myapplication.Person@7e7be6f
2020-03-26 18:39:10.460 7194-7194/com.example.myapplication D/PRETTY_LOGGER: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Name
可以给依赖标注名称。
比如项目中要访问两个服务器,就需要维护两个 retrofit 实例,用 @Name 来区分。
网友评论