Koin-7-Starting_Koin
https://start.insert-koin.io/#/getting-started/starting-koin
开始
Koin in 5 minutes (5分钟快速入手Koin)
Getting Started (开始)
- Starting Koin(开始使用Koin)
- Modules & definitions(模块和定义)
- Koin Components(Koin组件)
- Testing(测试)
- Koin for Java
- Koin for Android
Koin is a DSL, a container & a pragamtic API to leverage your dependencies.
Koin是一个DSL、一个容器、一个实用的API来有效利用我们的依赖
Koin DSL包含这些:
- KoinApplication DSL: 描述如何配置你的Koin Application
- Module DSL: 描述你的定义
开始使用Koin可以通过如下几种使用 startKoin
的形式:
StartKoin
在一个Kotlin文件中:
1 | fun main(vararg args: String) { |
Starting for Android
在任何一个Android类中:
1 | class MainApplication : Application() { |
如果你不能注入Android context或者application,那就确保在你的Koin application声明中使用 androidContext()
函数。
Starting for Ktor
Starting Koin from your Application
extension function:
1 | fun Application.main() { |
自定义Koin实例
Here below are the KoinApplication builders:
下面这些是KoinApplicatioon builders(构造者):
startKoin { }
- 创建并注册如下的KoinApplication实例koinApplication { }
- 创建KoinApplication实例
1 | // Create and register following KoinApplication instance |
Logging
开始时,Koin log就需要被定义他的名字或类型(如果log是活跃状态):(At start, Koin log what definition is bound by name or type (if log is activated):)
1 | [INFO] [Koin] bind type:'org.koin.example.CoffeeMaker' ~ [type:Single,class:'org.koin.example.CoffeeMaker'] |
DSL
快速回顾一下Koin DSL关键字:
startKoin { }
- 创建和注册如下的KoinApplication实例koinApplication { }
- 创建KoinApplication实例modules(...)
- 声明使用的moduleslogger()
- 声明Printloggerproperties(...)
- 声明map属性fileProperties()
- 从文件中使用属性environmentProperties()
- 从环境中使用属性androidLogger()
- 声明 Android Koin loggerandroidContext(...)
- 使用给出的Android contextandroidFileProperties()
- 使用 Android assets 中的属性文件slf4jLogger(...)
- 使用SLF4J Logger
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小柯基!