-
Notifications
You must be signed in to change notification settings - Fork 139
/
ConfigurationDsl.kt
84 lines (75 loc) · 3 KB
/
ConfigurationDsl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.springframework.fu.kofu
import org.springframework.beans.BeanUtils
import org.springframework.boot.context.properties.FunctionalConfigurationPropertiesBinder
import org.springframework.boot.context.properties.bind.Bindable
import org.springframework.context.ApplicationEvent
import org.springframework.context.support.BeanDefinitionDsl
import org.springframework.context.support.GenericApplicationContext
import org.springframework.context.support.registerBean
/**
* Kofu DSL for a configuration that can be imported in an application or used in tests.
*
* @see configuration
* @see enable
* @sample org.springframework.fu.kofu.samples.applicationDslWithConfiguration
* @author Sebastien Deleuze
*/
open class ConfigurationDsl(private val dsl: ConfigurationDsl.() -> Unit): AbstractDsl() {
/**
* Configure beans via a [dedicated DSL][BeanDefinitionDsl].
*
* Since classes with a single constructor have their parameters automatically autowired,
* it is recommended to use constructor injection with `val` read-only
* (and non-nullable when possible) private [properties](https://kotlinlang.org/docs/reference/properties.html).
*
* @sample org.springframework.fu.kofu.samples.beansDsl
*/
fun beans(dsl: BeanDefinitionDsl.() -> Unit) {
org.springframework.context.support.beans(dsl).initialize(context)
}
/**
* Enable the specified functional configuration.
* @see configuration
* @sample org.springframework.fu.kofu.samples.applicationDslWithConfiguration
*/
fun enable(configuration: AbstractDsl) {
configuration.initialize(context)
}
/**
* Bind and return the specified configuration properties class. If you need a bean,
* register it via the [beans] DSL.
*
* @sample org.springframework.fu.kofu.samples.configurationProperties
*/
inline fun <reified T : Any> configurationProperties(properties: T? = null, prefix: String = ""): T {
val bindedProperties = properties ?: FunctionalConfigurationPropertiesBinder(context).bind(prefix, Bindable.of(T::class.java)).orElseGet { BeanUtils.instantiateClass(T::class.java) }
context.registerBean<T>("${T::class.java.simpleName.toLowerCase()}ConfigurationProperties") {
bindedProperties
}
return bindedProperties
}
/**
* Configure global, package or class logging via a [dedicated DSL][LoggingDsl].
* @sample org.springframework.fu.kofu.samples.loggingDsl
*/
fun logging(dsl: LoggingDsl.() -> Unit) {
LoggingDsl(dsl).initialize(context)
}
/**
* Declare application event Listeners in order to run tasks when `ApplicationEvent`
* like `ApplicationReadyEvent` are emitted.
* @sample org.springframework.fu.kofu.samples.listener
*/
inline fun <reified E : ApplicationEvent>listener(crossinline listener: BeanDefinitionContext.(E) -> Unit) {
context.addApplicationListener {
// TODO Leverage SPR-16872 when it will be fixed
if (it is E) {
listener.invoke(BeanDefinitionContext(context), it)
}
}
}
override fun initialize(context: GenericApplicationContext) {
super.initialize(context)
dsl()
}
}