Commit d8f7850e authored by Arian Stolwijk's avatar Arian Stolwijk

Add examples for Dependency Injection

parent 7110e471
Pipeline #29219 failed with stages
in 33 seconds
plugins {
id("java")
}
plugins {
java
kotlin("jvm") version "1.3.61"
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation("org.springframework:spring-core:5.2.3.RELEASE")
implementation("org.springframework:spring-context:5.2.3.RELEASE")
}
repositories {
mavenCentral()
}
tasks {
compileKotlin.get().kotlinOptions {
jvmTarget = "1.8"
}
compileTestKotlin.get().kotlinOptions {
jvmTarget = "1.8"
}
}
rootProject.name = 'spring-core'
rootProject.name = "spring-core"
import kotlin.reflect.KClass
class DataSource {
fun <T : Any> execute(q: String, type: KClass<T>): T {
val value: Any = when (type) {
User::class -> User(1)
Product::class -> Product(2)
else -> error("unkown type")
}
return value as T
}
}
data class User(val id: Int)
data class Product(val id: Int)
package ioc
import DataSource
import Product
import User
class UserDao(private val dataSource: DataSource) {
fun getUser(): User = dataSource.execute("select user", User::class)
}
class ProductDao(private val dataSource: DataSource) {
fun getProduct(): Product = dataSource.execute("select product", Product::class)
}
fun main() {
val datasource = DataSource()
val productDao = ProductDao(datasource)
val userDao = UserDao(datasource)
println(userDao.getUser())
}
package singleton
import DataSource
import Product
import User
object DataSourceHolder {
val INSTANCE: DataSource by lazy { DataSource() }
}
class UserDao {
fun getUser(): User = DataSourceHolder.INSTANCE.execute("select user", User::class)
}
class ProductDao() {
fun getProduct(): Product = DataSourceHolder.INSTANCE.execute("select product", Product::class)
}
fun main() {
val datasource = DataSource()
val productDao = ProductDao()
val userDao = UserDao()
println(userDao.getUser())
}
package spring.beans
import DataSource
import ioc.ProductDao
import ioc.UserDao
import org.springframework.beans.factory.getBean
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
open class Config {
@Bean
open fun dataSource() = DataSource()
@Bean
open fun userDao(dataSource: DataSource) = UserDao(dataSource)
@Bean
open fun productDao(dataSource: DataSource) = ProductDao(dataSource)
}
fun main() {
val context = AnnotationConfigApplicationContext().apply {
register(Config::class.java)
refresh()
}
println(context.getBean<UserDao>().getUser())
}
\ No newline at end of file
package spring.components
import DataSource
import Product
import User
import ioc.UserDao
import org.springframework.beans.factory.getBean
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.stereotype.Component
import spring.beans.Config
@Component
class UserDao(private val dataSource: DataSource) {
fun getUser(): User = dataSource.execute("select user", User::class)
}
@Component
class ProductDao(private val dataSource: DataSource) {
fun getProduct(): Product = dataSource.execute("select product", Product::class)
}
@Configuration
@ComponentScan("spring.components")
open class Config {
@Bean
open fun dataSource() = DataSource()
}
fun main() {
val context = AnnotationConfigApplicationContext().apply {
register(Config::class.java)
refresh()
}
println(context.getBean<UserDao>().getUser())
}
\ No newline at end of file
package spring.manual
import DataSource
import ioc.ProductDao
import ioc.UserDao
import org.springframework.beans.factory.getBean
import org.springframework.context.support.GenericApplicationContext
import org.springframework.context.support.registerBean
fun main() {
val context = GenericApplicationContext().apply {
registerBean<DataSource>()
registerBean<UserDao>()
registerBean<ProductDao>()
refresh()
}
println(context.getBean<UserDao>().getUser())
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment