@@ -20,26 +20,99 @@ import org.springframework.beans.factory.config.BeanDefinitionCustomizer
20
20
import org.springframework.core.env.ConfigurableEnvironment
21
21
import java.util.function.Supplier
22
22
23
+ /* *
24
+ * Functional bean definition Kotlin DSL.
25
+ *
26
+ * Example:
27
+ *
28
+ * ```
29
+ * beans {
30
+ * bean<UserHandler>()
31
+ * bean {
32
+ * Routes(ref(), ref())
33
+ * }
34
+ * bean<WebHandler>("webHandler") {
35
+ * RouterFunctions.toWebHandler(
36
+ * ref<Routes>().router(),
37
+ * HandlerStrategies.builder().viewResolver(ref()).build())
38
+ * }
39
+ * bean("messageSource") {
40
+ * ReloadableResourceBundleMessageSource().apply {
41
+ * setBasename("messages")
42
+ * setDefaultEncoding("UTF-8")
43
+ * }
44
+ * }
45
+ * bean {
46
+ * val prefix = "classpath:/templates/"
47
+ * val suffix = ".mustache"
48
+ * val loader = MustacheResourceTemplateLoader(prefix, suffix)
49
+ * MustacheViewResolver(Mustache.compiler().withLoader(loader)).apply {
50
+ * setPrefix(prefix)
51
+ * setSuffix(suffix)
52
+ * }
53
+ * }
54
+ * profile("foo") {
55
+ * bean<Foo>()
56
+ * }
57
+ * }
58
+ * ```
59
+ *
60
+ * @author Sebastien Deleuze
61
+ * @see BeanDefinitionDsl
62
+ * @since 5.0
63
+ */
64
+ fun beans (init : BeanDefinitionDsl .() -> Unit ): BeanDefinitionDsl {
65
+ val beans = BeanDefinitionDsl ()
66
+ beans.init ()
67
+ return beans
68
+ }
69
+
23
70
/* *
24
71
* Class implementing functional bean definition Kotlin DSL.
25
72
*
73
+ * @constructor Create a new bean definition DSL.
74
+ * @param condition the predicate to fulfill in order to take in account the inner bean definition block
26
75
* @author Sebastien Deleuze
27
76
* @since 5.0
28
77
*/
29
- open class BeanDefinitionDsl (val condition : (ConfigurableEnvironment ) -> Boolean = { true }) : (GenericApplicationContext ) -> Unit {
78
+ class BeanDefinitionDsl (private val condition : (ConfigurableEnvironment ) -> Boolean = { true }) : (GenericApplicationContext ) -> Unit {
30
79
31
- protected val registrations = arrayListOf< (GenericApplicationContext ) -> Unit > ()
32
-
33
- protected val children = arrayListOf<BeanDefinitionDsl >()
34
-
80
+ @PublishedApi
81
+ internal val registrations = arrayListOf< (GenericApplicationContext ) -> Unit > ()
82
+
83
+ @PublishedApi
84
+ internal val children = arrayListOf<BeanDefinitionDsl >()
85
+
86
+ /* *
87
+ * Scope enum constants.
88
+ */
35
89
enum class Scope {
90
+ /* *
91
+ * Scope constant for the standard singleton scope
92
+ * @see org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON
93
+ */
36
94
SINGLETON ,
95
+ /* *
96
+ * Scope constant for the standard singleton scope
97
+ * @see org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE
98
+ */
37
99
PROTOTYPE
38
100
}
39
101
40
- class BeanDefinitionContext (val context : GenericApplicationContext ) {
102
+ /* *
103
+ * Provide read access to some application context facilities.
104
+ * @constructor Create a new bean definition context.
105
+ * @param context the `ApplicationContext` instance to use for retrieving bean references, `Environment`, etc.
106
+ */
107
+ inner class BeanDefinitionContext (@PublishedApi internal val context : GenericApplicationContext ) {
41
108
42
-
109
+ /* *
110
+ * Get a reference to the bean by type or type + name with the syntax
111
+ * `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
112
+ * it could be as short as `ref()` or `ref("foo")`.
113
+ * @param name the name of the bean to retrieve
114
+ * @param T type the bean must match, can be an interface or superclass
115
+ */
43
116
inline fun <reified T : Any > ref (name : String? = null) : T = when (name) {
44
117
null -> context.getBean(T ::class .java)
45
118
else -> context.getBean(name, T ::class .java)
@@ -55,8 +128,14 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
55
128
56
129
/* *
57
130
* Declare a bean definition from the given bean class which can be inferred when possible.
58
- *
59
- * @See GenericApplicationContext.registerBean
131
+ *
132
+ * @param name the name of the bean
133
+ * @param scope Override the target scope of this bean, specifying a new scope name.
134
+ * @param isLazyInit Set whether this bean should be lazily initialized.
135
+ * @param isPrimary Set whether this bean is a primary autowire candidate.
136
+ * @param isAutowireCandidate Set whether this bean is a candidate for getting autowired into some other bean.
137
+ * @see GenericApplicationContext.registerBean
138
+ * @see org.springframework.beans.factory.config.BeanDefinition
60
139
*/
61
140
inline fun <reified T : Any > bean (name : String? = null,
62
141
scope : Scope ? = null,
@@ -82,7 +161,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
82
161
/* *
83
162
* Declare a bean definition using the given supplier for obtaining a new instance.
84
163
*
85
- * @See GenericApplicationContext.registerBean
164
+ * @see GenericApplicationContext.registerBean
86
165
*/
87
166
inline fun <reified T : Any > bean (name : String? = null,
88
167
scope : Scope ? = null,
@@ -97,7 +176,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
97
176
isPrimary?.let { bd.isPrimary = isPrimary }
98
177
isAutowireCandidate?.let { bd.isAutowireCandidate = isAutowireCandidate }
99
178
}
100
-
179
+
101
180
registrations.add {
102
181
val beanContext = BeanDefinitionContext (it)
103
182
when (name) {
@@ -121,6 +200,7 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
121
200
/* *
122
201
* Take in account bean definitions enclosed in the provided lambda only when the
123
202
* specified environment-based predicate is true.
203
+ * @param condition the predicate to fulfill in order to take in account the inner bean definition block
124
204
*/
125
205
fun environment (condition : ConfigurableEnvironment .() -> Boolean , init : BeanDefinitionDsl .() -> Unit ): BeanDefinitionDsl {
126
206
val beans = BeanDefinitionDsl (condition::invoke)
@@ -129,6 +209,10 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
129
209
return beans
130
210
}
131
211
212
+ /* *
213
+ * Register the bean defined via the DSL on thAh pe provided application context.
214
+ * @param context The `ApplicationContext` to use for registering the beans
215
+ */
132
216
override fun invoke (context : GenericApplicationContext ) {
133
217
for (registration in registrations) {
134
218
if (condition.invoke(context.environment)) {
@@ -140,15 +224,3 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
140
224
}
141
225
}
142
226
}
143
-
144
- /* *
145
- * Functional bean definition Kotlin DSL.
146
- *
147
- * @author Sebastien Deleuze
148
- * @since 5.0
149
- */
150
- fun beans (init : BeanDefinitionDsl .() -> Unit ): BeanDefinitionDsl {
151
- val beans = BeanDefinitionDsl ()
152
- beans.init ()
153
- return beans
154
- }
0 commit comments