为什么使用依赖注入
A 依赖 B,B 依赖 C,C 依赖 D...
使用依赖注入后,只需要关注 B 。
为什么使用 Dagger2
大家都在用。
Inject 和 Component
最简单的例子。
// @Inject 放在 constructor 前表示将 Person 声明为依赖
class Person @Inject constructor() {
fun drink() {
Logger.d("drink")
}
}
// Component 按我的理解就是依赖注入器
@Component
interface AComponent {
// 方法名随意
fun giveMeAPerson(): Person
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
val aComponent = DaggerAComponent.create()
val person = aComponent.giveMeAPerson() // 从可用依赖中找到了 Person
person.drink()
}
}
2020-03-25 20:11:50.428 31571-31571/com.example.myapplication D/PRETTY_LOGGER: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2020-03-25 20:11:50.428 31571-31571/com.example.myapplication D/PRETTY_LOGGER: │ Thread: main
2020-03-25 20:11:50.428 31571-31571/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-25 20:11:50.429 31571-31571/com.example.myapplication D/PRETTY_LOGGER: │ MainActivity.onCreate (MainActivity.kt:15)
2020-03-25 20:11:50.430 31571-31571/com.example.myapplication D/PRETTY_LOGGER: │ Person.drink (Person.kt:9)
2020-03-25 20:11:50.430 31571-31571/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-25 20:11:50.431 31571-31571/com.example.myapplication D/PRETTY_LOGGER: │ drink
2020-03-25 20:11:50.431 31571-31571/com.example.myapplication D/PRETTY_LOGGER: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Inject 和 Component 的第二种用法
Inject 还能表明属性需要注入,修改代码如下。
@Component
interface AComponent {
// 方法名随意
fun iCanInject(a: MainActivity)
fun giveMeAPerson(): Person
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val aComponent = DaggerAComponent.create()
aComponent.iCanInject(this) // 依赖注入
person.drink()
}
@Inject
lateinit var person: Person
}
日志一样。
Drink What ?
那个人在喝什么,茶吗,红茶还是绿茶,修改代码如下。
interface Tea
class Person @Inject constructor(private val tea: Tea) {
fun drink() {
Logger.d("drink $tea")
}
}
运行程序,报错。
Tea cannot be provided without an @Provides-annotated method.
意思就是依赖中找不到 Tea,而接口又没有构造函数,无法声明为依赖。
Module 和 Provides
修改代码。
class RedTea : Tea
@Module
class AModule {
@Provides
fun provideATea(): Tea {
return RedTea()
}
}
// 表明还可以从 AModule 中寻找依赖。
@Component(modules = [AModule::class])
interface AComponent {
fun iCanInject(a: MainActivity)
fun giveMeAPerson(): Person
}
2020-03-25 20:53:40.038 32333-32333/com.example.myapplication D/PRETTY_LOGGER: ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2020-03-25 20:53:40.038 32333-32333/com.example.myapplication D/PRETTY_LOGGER: │ Thread: main
2020-03-25 20:53:40.038 32333-32333/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-25 20:53:40.039 32333-32333/com.example.myapplication D/PRETTY_LOGGER: │ MainActivity.onCreate (MainActivity.kt:15)
2020-03-25 20:53:40.040 32333-32333/com.example.myapplication D/PRETTY_LOGGER: │ Person.drink (Person.kt:9)
2020-03-25 20:53:40.040 32333-32333/com.example.myapplication D/PRETTY_LOGGER: ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
2020-03-25 20:53:40.040 32333-32333/com.example.myapplication D/PRETTY_LOGGER: │ drink com.example.myapplication.RedTea@8e6fb69
2020-03-25 20:53:40.040 32333-32333/com.example.myapplication D/PRETTY_LOGGER: └────────────────────────────────────────────────────────────────────────────────────────────────────────────────









网友评论