Skip to content

Commit 394a523

Browse files
committed
KTLN-841: add samples for annotated koin modules and definitions
1 parent 5a15197 commit 394a523

File tree

4 files changed

+125
-13
lines changed

4 files changed

+125
-13
lines changed

koin-guide/koin-annotations/build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
group = "com.baeldung"
22
version = "1.0-SNAPSHOT"
33

4-
val koinVersion = "3.5.6"
5-
val koinAnnotationsVersion = "1.3.1"
4+
val koinVersion = "4.0.0-RC1"
5+
val koinAnnotationsVersion = "1.4.0-RC3"
66

77
repositories {
88
mavenCentral()
@@ -14,13 +14,13 @@ plugins {
1414
}
1515

1616
dependencies {
17-
implementation("io.insert-koin:koin-core:$koinVersion")
17+
implementation("io.insert-koin:koin-core-jvm:$koinVersion")
1818
implementation("io.insert-koin:koin-annotations:$koinAnnotationsVersion")
1919
ksp("io.insert-koin:koin-ksp-compiler:$koinAnnotationsVersion")
2020
}
2121

22-
sourceSets.main {
23-
java.srcDirs("build/generated/ksp/main/kotlin")
22+
ksp {
23+
arg("KOIN_CONFIG_CHECK", "true")
2424
}
2525

2626
tasks.test {

koin-guide/koin-annotations/src/main/kotlin/com/baeldung/kotlin/koin/annotations/KoinAnnotationsApplication.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.baeldung.kotlin.koin.annotations
2+
3+
import org.koin.core.annotation.ComponentScan
4+
import org.koin.core.annotation.Factory
5+
import org.koin.core.annotation.KoinInternalApi
6+
import org.koin.core.annotation.Module
7+
import org.koin.core.annotation.Single
8+
import org.koin.core.context.startKoin
9+
import org.koin.ksp.generated.defaultModule
10+
import org.koin.ksp.generated.module
11+
import kotlin.random.Random
12+
13+
@Single
14+
class NumberService {
15+
16+
fun generateRandomNumber(): Int = Random.nextInt()
17+
}
18+
19+
object KoinDefaultModuleApplication {
20+
21+
@JvmStatic
22+
fun main(args: Array<String>) {
23+
val koinApp = startKoin {
24+
printLogger()
25+
modules(defaultModule)
26+
}
27+
28+
require(koinApp.koin.getAll<NumberService>().isNotEmpty())
29+
}
30+
}
31+
32+
interface Database
33+
class DatabaseImpl : Database
34+
35+
interface Repository
36+
class RepositoryImpl(database: Database) : Repository
37+
38+
@Module
39+
class DaoModule {
40+
41+
@Single
42+
fun provideDatabase(): Database {
43+
return DatabaseImpl()
44+
}
45+
46+
@Factory
47+
fun provideRepository(database: Database): Repository {
48+
return RepositoryImpl(database)
49+
}
50+
}
51+
52+
object KoinAnnotationsApplication {
53+
54+
@JvmStatic
55+
fun main(args: Array<String>) {
56+
val koinApp = startKoin {
57+
printLogger()
58+
modules(DaoModule().module)
59+
}
60+
61+
require(koinApp.koin.getAll<Database>().isNotEmpty())
62+
require(koinApp.koin.getAll<Repository>().isNotEmpty())
63+
}
64+
}
65+
66+
class ModuleAComponent
67+
68+
@Module
69+
class ModuleA {
70+
71+
@Single
72+
fun provideModuleAComponent(): ModuleAComponent {
73+
return ModuleAComponent()
74+
}
75+
}
76+
77+
@Module(includes = [ModuleA::class])
78+
class ModuleB
79+
80+
object KoinModulesCompositionApplication {
81+
82+
@JvmStatic
83+
fun main(args: Array<String>) {
84+
val koinApp = startKoin {
85+
printLogger()
86+
modules(ModuleB().module)
87+
}
88+
89+
require(koinApp.koin.getAll<ModuleAComponent>().isNotEmpty())
90+
}
91+
}
92+
93+
@Module
94+
@ComponentScan("com.baeldung.kotlin.koin.annotations.domain")
95+
class DomainModule
96+
97+
object KoinScannedComponentsModulesApplication {
98+
99+
@OptIn(KoinInternalApi::class)
100+
@JvmStatic
101+
fun main(args: Array<String>) {
102+
val koinApp = startKoin {
103+
printLogger()
104+
modules(DomainModule().module)
105+
}
106+
107+
require(koinApp.koin.instanceRegistry.instances.size == 2)
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.kotlin.koin.annotations.domain
2+
3+
import org.koin.core.annotation.Factory
4+
import org.koin.core.annotation.Single
5+
6+
@Single
7+
class ScannedSingletonComponent
8+
9+
@Factory
10+
class ScannedFactoryComponent
11+

0 commit comments

Comments
 (0)