From d7bbfb353d77401675c86d7480093068a7ba6b45 Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Mon, 29 Oct 2018 21:04:07 +0000 Subject: [PATCH] Examples of using Injekt (#5519) --- core-kotlin/pom.xml | 5 ++ .../injekt/DelegateInjectionApplication.kt | 60 +++++++++++++++++++ .../com/baeldung/injekt/KeyedApplication.kt | 37 ++++++++++++ .../com/baeldung/injekt/ModularApplication.kt | 46 ++++++++++++++ .../baeldung/injekt/PerThreadApplication.kt | 48 +++++++++++++++ .../com/baeldung/injekt/SimpleApplication.kt | 34 +++++++++++ 6 files changed, 230 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/injekt/DelegateInjectionApplication.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/injekt/KeyedApplication.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/injekt/ModularApplication.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/injekt/PerThreadApplication.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/injekt/SimpleApplication.kt diff --git a/core-kotlin/pom.xml b/core-kotlin/pom.xml index 0894a57320..5cdb5f700e 100644 --- a/core-kotlin/pom.xml +++ b/core-kotlin/pom.xml @@ -61,6 +61,11 @@ 3.3.0 pom + + uy.kohesive.injekt + injekt-core + 1.16.1 + diff --git a/core-kotlin/src/main/kotlin/com/baeldung/injekt/DelegateInjectionApplication.kt b/core-kotlin/src/main/kotlin/com/baeldung/injekt/DelegateInjectionApplication.kt new file mode 100644 index 0000000000..fb9beda621 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/injekt/DelegateInjectionApplication.kt @@ -0,0 +1,60 @@ +package com.baeldung.injekt + +import org.slf4j.LoggerFactory +import uy.kohesive.injekt.* +import uy.kohesive.injekt.api.* +import java.util.* + +class DelegateInjectionApplication { + companion object : InjektMain() { + private val LOG = LoggerFactory.getLogger(DelegateInjectionApplication::class.java) + @JvmStatic fun main(args: Array) { + DelegateInjectionApplication().run() + } + + override fun InjektRegistrar.registerInjectables() { + addFactory { + val value = FactoryInstance("Factory" + UUID.randomUUID().toString()) + LOG.info("Constructing instance: {}", value) + value + } + + addSingletonFactory { + val value = SingletonInstance("Singleton" + UUID.randomUUID().toString()) + LOG.info("Constructing singleton instance: {}", value) + value + } + + addSingletonFactory { App() } + } + } + + data class FactoryInstance(val value: String) + data class SingletonInstance(val value: String) + + class App { + private val instance: FactoryInstance by injectValue() + private val lazyInstance: FactoryInstance by injectLazy() + private val singleton: SingletonInstance by injectValue() + private val lazySingleton: SingletonInstance by injectLazy() + + fun run() { + for (i in 1..5) { + LOG.info("Instance {}: {}", i, instance) + } + for (i in 1..5) { + LOG.info("Lazy Instance {}: {}", i, lazyInstance) + } + for (i in 1..5) { + LOG.info("Singleton {}: {}", i, singleton) + } + for (i in 1..5) { + LOG.info("Lazy Singleton {}: {}", i, lazySingleton) + } + } + } + + fun run() { + Injekt.get().run() + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/injekt/KeyedApplication.kt b/core-kotlin/src/main/kotlin/com/baeldung/injekt/KeyedApplication.kt new file mode 100644 index 0000000000..744459b7fe --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/injekt/KeyedApplication.kt @@ -0,0 +1,37 @@ +package com.baeldung.injekt + +import org.slf4j.LoggerFactory +import uy.kohesive.injekt.* +import uy.kohesive.injekt.api.* + +class KeyedApplication { + companion object : InjektMain() { + private val LOG = LoggerFactory.getLogger(KeyedApplication::class.java) + @JvmStatic fun main(args: Array) { + KeyedApplication().run() + } + + override fun InjektRegistrar.registerInjectables() { + val configs = mapOf( + "google" to Config("googleClientId", "googleClientSecret"), + "twitter" to Config("twitterClientId", "twitterClientSecret") + ) + addPerKeyFactory {key -> configs[key]!! } + + addSingletonFactory { App() } + } + } + + data class Config(val clientId: String, val clientSecret: String) + + class App { + fun run() { + LOG.info("Google config: {}", Injekt.get("google")) + LOG.info("Twitter config: {}", Injekt.get("twitter")) + } + } + + fun run() { + Injekt.get().run() + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/injekt/ModularApplication.kt b/core-kotlin/src/main/kotlin/com/baeldung/injekt/ModularApplication.kt new file mode 100644 index 0000000000..e802f3f6d5 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/injekt/ModularApplication.kt @@ -0,0 +1,46 @@ +package com.baeldung.injekt + +import org.slf4j.LoggerFactory +import uy.kohesive.injekt.* +import uy.kohesive.injekt.api.* + +class ModularApplication { + class ConfigModule(private val port: Int) : InjektModule { + override fun InjektRegistrar.registerInjectables() { + addSingleton(Config(port)) + } + } + + object ServerModule : InjektModule { + override fun InjektRegistrar.registerInjectables() { + addSingletonFactory { Server(Injekt.get()) } + } + } + + companion object : InjektMain() { + private val LOG = LoggerFactory.getLogger(Server::class.java) + @JvmStatic fun main(args: Array) { + ModularApplication().run() + } + + override fun InjektRegistrar.registerInjectables() { + importModule(ConfigModule(12345)) + importModule(ServerModule) + } + } + + data class Config( + val port: Int + ) + + class Server(private val config: Config) { + + fun start() { + LOG.info("Starting server on ${config.port}") + } + } + + fun run() { + Injekt.get().start() + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/injekt/PerThreadApplication.kt b/core-kotlin/src/main/kotlin/com/baeldung/injekt/PerThreadApplication.kt new file mode 100644 index 0000000000..a42f314349 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/injekt/PerThreadApplication.kt @@ -0,0 +1,48 @@ +package com.baeldung.injekt + +import org.slf4j.LoggerFactory +import uy.kohesive.injekt.* +import uy.kohesive.injekt.api.* +import java.util.* +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit + +class PerThreadApplication { + companion object : InjektMain() { + private val LOG = LoggerFactory.getLogger(PerThreadApplication::class.java) + @JvmStatic fun main(args: Array) { + PerThreadApplication().run() + } + + override fun InjektRegistrar.registerInjectables() { + addPerThreadFactory { + val value = FactoryInstance(UUID.randomUUID().toString()) + LOG.info("Constructing instance: {}", value) + value + } + + addSingletonFactory { App() } + } + } + + data class FactoryInstance(val value: String) + + class App { + fun run() { + val threadPool = Executors.newFixedThreadPool(5) + + for (i in 1..20) { + threadPool.submit { + val instance = Injekt.get() + LOG.info("Value for thread {}: {}", Thread.currentThread().id, instance) + TimeUnit.MILLISECONDS.sleep(100) + } + } + threadPool.awaitTermination(10, TimeUnit.SECONDS) + } + } + + fun run() { + Injekt.get().run() + } +} diff --git a/core-kotlin/src/main/kotlin/com/baeldung/injekt/SimpleApplication.kt b/core-kotlin/src/main/kotlin/com/baeldung/injekt/SimpleApplication.kt new file mode 100644 index 0000000000..2b07cd059f --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/injekt/SimpleApplication.kt @@ -0,0 +1,34 @@ +package com.baeldung.injekt + +import org.slf4j.LoggerFactory +import uy.kohesive.injekt.* +import uy.kohesive.injekt.api.* + +class SimpleApplication { + companion object : InjektMain() { + private val LOG = LoggerFactory.getLogger(Server::class.java) + @JvmStatic fun main(args: Array) { + SimpleApplication().run() + } + + override fun InjektRegistrar.registerInjectables() { + addSingleton(Config(12345)) + addSingletonFactory { Server(Injekt.get()) } + } + } + + data class Config( + val port: Int + ) + + class Server(private val config: Config) { + + fun start() { + LOG.info("Starting server on ${config.port}") + } + } + + fun run() { + Injekt.get().start() + } +}