在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):misfitlabsdev/kotlin-guice开源软件地址(OpenSource Url):https://github.com/misfitlabsdev/kotlin-guice开源编程语言(OpenSource Language):Kotlin 93.7%开源软件介绍(OpenSource Introduction):kotlin-guiceGuice extensions for Kotlin. This provides extension wrappers and extension methods for providing a better Guice DSL experience from Kotlin. It takes advantage of reified types to reduce class references like DownloadDownload the latest JAR via Maven: <dependency>
<groupId>dev.misfitlabs.kotlinguice4</groupId>
<artifactId>kotlin-guice</artifactId>
<version>1.6.0</version>
</dependency> or Gradle: compile 'dev.misfitlabs.kotlinguice4:kotlin-guice:1.6.0' Getting StartedKotlinModuleUse import dev.misfitlabs.kotlinguice4.KotlinModule
class MyModule : KotlinModule() {
override fun configure() {
bind<Service>().to<ServiceImpl>().`in`<Singleton>()
bind<PaymentService<CreditCard>>().to<CreditCardPaymentService>()
bind<CreditCardProcessor>().annotatedWith<PayPal>().to<PayPalCreditCardProcessor>()
}
} The import dev.misfitlabs.kotlinguice4.KotlinPrivateModule
class MyPrivateModule : KotlinPrivateModule() {
override fun configure() {
bind<Service>().to<ServiceImpl>().`in`<Singleton>()
bind<PaymentService<CreditCard>>().to<CreditCardPaymentService>()
bind<CreditCardProcessor>().annotatedWith<PayPal>().to<PayPalCreditCardProcessor>()
expose<PaymentService<CreditCard>>()
}
} InjectorThe Guice injector has been enhanced with extension methods to make direct use of the injector better from Kotlin. import dev.misfitlabs.kotlinguice4.annotatedKey
import dev.misfitlabs.kotlinguice4.getInstance
fun main(args: Array<String>) {
val injector = Guice.createInjector(MyModule(), MyPrivateModule())
val paymentService = injector.getInstance<PaymentService<CreditCard>>()
// Use the annotatedKey to get an annotated instance
val payPalProcessor = injector.getInstance(annotatedKey<CreditCardProcessor, PayPayl>())
} Key and TypeLiteralPackage level functions are included to enhance creating import dev.misfitlabs.kotlinguice4.annotatedKey
import dev.misfitlabs.kotlinguice4.key
import dev.misfitlabs.kotlinguice4.typeLiteral
val key = key<String>()
val annotatedKey = annotatedKey<String, SomeAnnotation>()
val sameAnnotatedDifferentKey = annotatedKey.getType<Long>()
val listType = typeLiteral<PaymentService<CreditCrd>>() MultibindingsAs of version 1.4.1 the kotlin-guice-multibindings module is gone and the functionality has been merged into kotlin-guice. Usageval multibinder = KotlinMultibinder.newSetBinder<Snack>(kotlinBinder)
multibinder.addBinding().to<Twix>()
val mapbinder = KotlinMapBinder.newMapBinder<String, Snack>(kotlinBinder)
mapbinder.addBinding("twix").to<Twix>() With Guice 4.2+, scanning for methods with the multibinding annotations install(KotlinMultibindingsScanner.asModule()) License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论