Commit e737a322 authored by Arian Stolwijk's avatar Arian Stolwijk
Browse files

Add more examples

parent ff848a28
......@@ -6,8 +6,8 @@ plugins {
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")
implementation("org.aspectj:aspectjweaver:1.9.3")
}
repositories {
......
......@@ -2,6 +2,7 @@ import kotlin.reflect.KClass
class DataSource {
fun <T : Any> execute(q: String, type: KClass<T>): T {
println("query: $q")
val value: Any = when (type) {
User::class -> User(1)
Product::class -> Product(2)
......
package spring.manual
package spring.a_manual
import DataSource
import ioc.ProductDao
......@@ -15,4 +15,4 @@ fun main() {
refresh()
}
println(context.getBean<UserDao>().getUser())
}
\ No newline at end of file
}
package spring.beans
package spring.b_beans
import DataSource
import ioc.ProductDao
......@@ -22,9 +22,6 @@ open class Config {
}
fun main() {
val context = AnnotationConfigApplicationContext().apply {
register(Config::class.java)
refresh()
}
val context = AnnotationConfigApplicationContext(Config::class.java)
println(context.getBean<UserDao>().getUser())
}
\ No newline at end of file
}
package spring.components
package spring.c_components
import DataSource
import Product
......@@ -10,7 +10,7 @@ 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
import spring.b_beans.Config
@Component
class UserDao(private val dataSource: DataSource) {
......@@ -23,16 +23,13 @@ class ProductDao(private val dataSource: DataSource) {
}
@Configuration
@ComponentScan("spring.components")
@ComponentScan
open class Config {
@Bean
open fun dataSource() = DataSource()
}
fun main() {
val context = AnnotationConfigApplicationContext().apply {
register(Config::class.java)
refresh()
}
val context = AnnotationConfigApplicationContext(Config::class.java)
println(context.getBean<UserDao>().getUser())
}
\ No newline at end of file
}
package spring.d_processors
import DataSource
import User
import org.springframework.beans.factory.config.BeanPostProcessor
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
@Component
open class UserDao(private val dataSource: DataSource) {
lateinit var query: String
open fun getUser(): User = dataSource.execute(query, User::class)
fun setQuery(s: String): UserDao {
this.query = s
return this
}
}
@Configuration
@ComponentScan
open class Config {
@Bean
open fun processor() = object : BeanPostProcessor {
override fun postProcessAfterInitialization(bean: Any, beanName: String) =
when (bean) {
is UserDao -> bean.setQuery("select users")
else -> bean
}.also {
println("Bean '$beanName' created : $bean")
}
}
@Bean
open fun dataSource() = DataSource()
}
fun main() {
val context = AnnotationConfigApplicationContext(Config::class.java)
println(context.getBean<UserDao>().getUser())
}
package spring.e_aop
import DataSource
import Product
import User
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.reflect.MethodSignature
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.context.annotation.EnableAspectJAutoProxy
import org.springframework.stereotype.Component
@Aspect
@Component
class TimerAspect {
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyTimer
@Around("@annotation(spring.e_aop.TimerAspect.MyTimer)")
fun timer(jp: ProceedingJoinPoint): Any {
val start = System.nanoTime()
val result = jp.proceed()
println("${System.nanoTime() - start} nano seconds")
return result
}
}
@Component
class GlobalCache {
private val cache = mutableMapOf<String, Any>()
fun computeIfAbsent(key: String, compute: (String) -> Any) = cache.computeIfAbsent(key, compute)
}
@Aspect
@Component
class CacheAspect(private val cache: GlobalCache) {
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyCache(val key: String)
@Around("@annotation(spring.e_aop.CacheAspect.MyCache)")
fun cache(jp: ProceedingJoinPoint): Any {
val signature = jp.signature as MethodSignature
val method = signature.method
val annotation: MyCache = method.getAnnotation(MyCache::class.java)
return cache.computeIfAbsent(annotation.key) { jp.proceed() }
}
}
@Component
open class UserDao(private val dataSource: DataSource) {
@TimerAspect.MyTimer
open fun getUser(): User = dataSource.execute("select user", User::class)
}
@Component
open class ProductDao(private val dataSource: DataSource) {
@CacheAspect.MyCache(key = "product")
open fun getProduct(): Product = dataSource.execute("select product", Product::class).also {
println("retrieving product")
}
}
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
open class Config {
@Bean
open fun dataSource() = DataSource()
}
fun main() {
val context = AnnotationConfigApplicationContext(Config::class.java)
println(context.getBean<UserDao>().getUser())
val productDao = context.getBean<ProductDao>()
println(productDao.getProduct())
println(productDao.getProduct())
}
package spring.f_environment
import org.springframework.context.support.GenericApplicationContext
import org.springframework.core.env.PropertySource
import org.springframework.core.env.get
class CustomPropertySource : PropertySource<Any>("custom") {
override fun getProperty(name: String): Any? {
return "kees"
}
}
fun main() {
val context = GenericApplicationContext()
val env = context.environment
env.propertySources.addLast(CustomPropertySource())
env.propertySources.forEach {
val source: Map<*, *>? = it.source as? Map<*, *>
println("source: ${it.name} has ${source?.size} properties")
}
println(env["database.url"])
println(env["fallback"])
}
package spring.f_environment
import org.springframework.context.support.GenericApplicationContext
import org.springframework.core.env.get
fun main() {
val context = GenericApplicationContext()
val env = context.environment
env.propertySources.forEach {
val source: Map<*, *>? = it.source as? Map<*, *>
println("source: ${it.name} has ${source?.size} properties")
}
println(env["database.url"])
println(env["foo"])
}
package spring.f_environment
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource
import org.springframework.core.env.get
@Configuration
@PropertySource("classpath:some.properties")
open class Config
fun main() {
val context = AnnotationConfigApplicationContext(Config::class.java)
context.environment.propertySources.forEach {
val source: Map<*, *>? = it.source as? Map<*, *>
println("source: ${it.name} has ${source?.size} properties")
}
println(context.environment["foo"])
}
package spring.f_environment
import org.springframework.beans.factory.annotation.Value
import org.springframework.beans.factory.getBean
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.support.registerBean
import org.springframework.core.env.MapPropertySource
class Database(private val url: String,
private val user: String,
private val pass: String) {
override fun toString() = "$user:$pass@$url"
}
@Configuration
open class ValueConfig {
@Bean
open fun database(
@Value("\${database.url}") databaseUrl: String,
@Value("\${database.user}") databaseUser: String,
@Value("\${database.pass}") databasePass: String
) = Database(databaseUrl, databaseUser, databasePass)
}
fun main() {
val context = AnnotationConfigApplicationContext().apply {
environment.propertySources.addLast(MapPropertySource("from map", mapOf(
"database.url" to "mysql://localhost:3306",
"database.user" to "kees",
"database.pass" to "secret"
)))
environment.addActiveProfile("foo")
registerBean<ValueConfig>()
refresh()
}
println(context.getBean<Database>())
}
foo=bar
\ 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