import org.koin.core.KoinComponent
import org.koin.core.context.startKoin
import org.koin.core.get
import org.koin.core.inject
import org.koin.dsl.module

data class HelloMessageData(val message: String = "Koin!")

interface HelloService {
    fun hello(): String
}
class HelloServiceImpl( private val helloMessageData: HelloMessageData) : HelloService {
    override fun hello() = "Hello, ${helloMessageData.message}"
}

class HiClass(private val service: HelloService) {
    fun hi() = "Hi...${service.hello()}"
}

//注入：定义（声明）各注入类的实例变量
object HelloApplication : KoinComponent {
    val helloService by inject<HelloService>()
    val hiClass1 by inject<HiClass>()
    val hiClass2: HiClass = get()
}

fun main() {
    startKoin {
        // declare modules
        modules(
            module {
                //实际生成实例
                single { HelloMessageData("Java") }
                single { HelloServiceImpl(get()) as HelloService }
                factory { HiClass(get()) }
            })
    }
    //实例使用
    println(HelloApplication.helloService.hello())
    println(HelloApplication.hiClass1.hi())
    println(HelloApplication.hiClass2.hi())

    //不用Koin依赖注入框架的情况
    val d=HelloMessageData("Kotlin")
    val h=HelloServiceImpl(d)
    val hc=HiClass(h)
    println(h.hello())
    println(hc.hi())
}

