Skip to content

Commit d7bbfb3

Browse files
sazzerpivovarit
authored andcommitted
Examples of using Injekt (eugenp#5519)
1 parent c028c40 commit d7bbfb3

6 files changed

Lines changed: 230 additions & 0 deletions

File tree

core-kotlin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<version>3.3.0</version>
6262
<type>pom</type>
6363
</dependency>
64+
<dependency>
65+
<groupId>uy.kohesive.injekt</groupId>
66+
<artifactId>injekt-core</artifactId>
67+
<version>1.16.1</version>
68+
</dependency>
6469
</dependencies>
6570

6671
<properties>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
import java.util.*
7+
8+
class DelegateInjectionApplication {
9+
companion object : InjektMain() {
10+
private val LOG = LoggerFactory.getLogger(DelegateInjectionApplication::class.java)
11+
@JvmStatic fun main(args: Array<String>) {
12+
DelegateInjectionApplication().run()
13+
}
14+
15+
override fun InjektRegistrar.registerInjectables() {
16+
addFactory {
17+
val value = FactoryInstance("Factory" + UUID.randomUUID().toString())
18+
LOG.info("Constructing instance: {}", value)
19+
value
20+
}
21+
22+
addSingletonFactory {
23+
val value = SingletonInstance("Singleton" + UUID.randomUUID().toString())
24+
LOG.info("Constructing singleton instance: {}", value)
25+
value
26+
}
27+
28+
addSingletonFactory { App() }
29+
}
30+
}
31+
32+
data class FactoryInstance(val value: String)
33+
data class SingletonInstance(val value: String)
34+
35+
class App {
36+
private val instance: FactoryInstance by injectValue()
37+
private val lazyInstance: FactoryInstance by injectLazy()
38+
private val singleton: SingletonInstance by injectValue()
39+
private val lazySingleton: SingletonInstance by injectLazy()
40+
41+
fun run() {
42+
for (i in 1..5) {
43+
LOG.info("Instance {}: {}", i, instance)
44+
}
45+
for (i in 1..5) {
46+
LOG.info("Lazy Instance {}: {}", i, lazyInstance)
47+
}
48+
for (i in 1..5) {
49+
LOG.info("Singleton {}: {}", i, singleton)
50+
}
51+
for (i in 1..5) {
52+
LOG.info("Lazy Singleton {}: {}", i, lazySingleton)
53+
}
54+
}
55+
}
56+
57+
fun run() {
58+
Injekt.get<App>().run()
59+
}
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
7+
class KeyedApplication {
8+
companion object : InjektMain() {
9+
private val LOG = LoggerFactory.getLogger(KeyedApplication::class.java)
10+
@JvmStatic fun main(args: Array<String>) {
11+
KeyedApplication().run()
12+
}
13+
14+
override fun InjektRegistrar.registerInjectables() {
15+
val configs = mapOf(
16+
"google" to Config("googleClientId", "googleClientSecret"),
17+
"twitter" to Config("twitterClientId", "twitterClientSecret")
18+
)
19+
addPerKeyFactory<Config, String> {key -> configs[key]!! }
20+
21+
addSingletonFactory { App() }
22+
}
23+
}
24+
25+
data class Config(val clientId: String, val clientSecret: String)
26+
27+
class App {
28+
fun run() {
29+
LOG.info("Google config: {}", Injekt.get<Config>("google"))
30+
LOG.info("Twitter config: {}", Injekt.get<Config>("twitter"))
31+
}
32+
}
33+
34+
fun run() {
35+
Injekt.get<App>().run()
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
7+
class ModularApplication {
8+
class ConfigModule(private val port: Int) : InjektModule {
9+
override fun InjektRegistrar.registerInjectables() {
10+
addSingleton(Config(port))
11+
}
12+
}
13+
14+
object ServerModule : InjektModule {
15+
override fun InjektRegistrar.registerInjectables() {
16+
addSingletonFactory { Server(Injekt.get()) }
17+
}
18+
}
19+
20+
companion object : InjektMain() {
21+
private val LOG = LoggerFactory.getLogger(Server::class.java)
22+
@JvmStatic fun main(args: Array<String>) {
23+
ModularApplication().run()
24+
}
25+
26+
override fun InjektRegistrar.registerInjectables() {
27+
importModule(ConfigModule(12345))
28+
importModule(ServerModule)
29+
}
30+
}
31+
32+
data class Config(
33+
val port: Int
34+
)
35+
36+
class Server(private val config: Config) {
37+
38+
fun start() {
39+
LOG.info("Starting server on ${config.port}")
40+
}
41+
}
42+
43+
fun run() {
44+
Injekt.get<Server>().start()
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
import java.util.*
7+
import java.util.concurrent.Executors
8+
import java.util.concurrent.TimeUnit
9+
10+
class PerThreadApplication {
11+
companion object : InjektMain() {
12+
private val LOG = LoggerFactory.getLogger(PerThreadApplication::class.java)
13+
@JvmStatic fun main(args: Array<String>) {
14+
PerThreadApplication().run()
15+
}
16+
17+
override fun InjektRegistrar.registerInjectables() {
18+
addPerThreadFactory {
19+
val value = FactoryInstance(UUID.randomUUID().toString())
20+
LOG.info("Constructing instance: {}", value)
21+
value
22+
}
23+
24+
addSingletonFactory { App() }
25+
}
26+
}
27+
28+
data class FactoryInstance(val value: String)
29+
30+
class App {
31+
fun run() {
32+
val threadPool = Executors.newFixedThreadPool(5)
33+
34+
for (i in 1..20) {
35+
threadPool.submit {
36+
val instance = Injekt.get<FactoryInstance>()
37+
LOG.info("Value for thread {}: {}", Thread.currentThread().id, instance)
38+
TimeUnit.MILLISECONDS.sleep(100)
39+
}
40+
}
41+
threadPool.awaitTermination(10, TimeUnit.SECONDS)
42+
}
43+
}
44+
45+
fun run() {
46+
Injekt.get<App>().run()
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.injekt
2+
3+
import org.slf4j.LoggerFactory
4+
import uy.kohesive.injekt.*
5+
import uy.kohesive.injekt.api.*
6+
7+
class SimpleApplication {
8+
companion object : InjektMain() {
9+
private val LOG = LoggerFactory.getLogger(Server::class.java)
10+
@JvmStatic fun main(args: Array<String>) {
11+
SimpleApplication().run()
12+
}
13+
14+
override fun InjektRegistrar.registerInjectables() {
15+
addSingleton(Config(12345))
16+
addSingletonFactory { Server(Injekt.get()) }
17+
}
18+
}
19+
20+
data class Config(
21+
val port: Int
22+
)
23+
24+
class Server(private val config: Config) {
25+
26+
fun start() {
27+
LOG.info("Starting server on ${config.port}")
28+
}
29+
}
30+
31+
fun run() {
32+
Injekt.get<Server>().start()
33+
}
34+
}

0 commit comments

Comments
 (0)