1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ import java.util.function.Supplier
63
63
*/
64
64
fun beans (init : BeanDefinitionDsl .() -> Unit ): BeanDefinitionDsl {
65
65
val beans = BeanDefinitionDsl ()
66
- beans.init ()
66
+ beans.init = init
67
67
return beans
68
68
}
69
69
@@ -79,12 +79,24 @@ fun beans(init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl {
79
79
open class BeanDefinitionDsl (private val condition : (ConfigurableEnvironment ) -> Boolean = { true })
80
80
: ApplicationContextInitializer <GenericApplicationContext > {
81
81
82
- @PublishedApi
83
- internal val registrations = arrayListOf< (GenericApplicationContext ) -> Unit > ()
84
-
85
82
@PublishedApi
86
83
internal val children = arrayListOf<BeanDefinitionDsl >()
87
84
85
+ internal lateinit var init : BeanDefinitionDsl .() -> Unit
86
+
87
+ /* *
88
+ * Access to the context for advanced use-cases.
89
+ * @since 5.1
90
+ */
91
+ lateinit var context: GenericApplicationContext
92
+
93
+ /* *
94
+ * Shortcut for `context.environment`
95
+ * @since 5.1
96
+ */
97
+ val env : ConfigurableEnvironment
98
+ get() = context.environment
99
+
88
100
/* *
89
101
* Scope enum constants.
90
102
*/
@@ -130,34 +142,6 @@ open class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) ->
130
142
131
143
}
132
144
133
- /* *
134
- * Provide read access to some application context facilities.
135
- * @constructor Create a new bean definition context.
136
- * @param context the `ApplicationContext` instance to use for retrieving bean
137
- * references, `Environment`, etc.
138
- */
139
- inner class BeanDefinitionContext (@PublishedApi internal val context : GenericApplicationContext ) {
140
-
141
- /* *
142
- * Get a reference to the bean by type or type + name with the syntax
143
- * `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
144
- * it could be as short as `ref()` or `ref("foo")`.
145
- * @param name the name of the bean to retrieve
146
- * @param T type the bean must match, can be an interface or superclass
147
- */
148
- inline fun <reified T : Any > ref (name : String? = null) : T = when (name) {
149
- null -> context.getBean(T ::class .java)
150
- else -> context.getBean(name, T ::class .java)
151
- }
152
-
153
- /* *
154
- * Get the [ConfigurableEnvironment] associated to the underlying [GenericApplicationContext].
155
- */
156
- val env : ConfigurableEnvironment
157
- get() = context.environment
158
-
159
- }
160
-
161
145
/* *
162
146
* Declare a bean definition from the given bean class which can be inferred when possible.
163
147
*
@@ -177,23 +161,22 @@ open class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) ->
177
161
isPrimary : Boolean? = null,
178
162
autowireMode : Autowire = Autowire .CONSTRUCTOR ,
179
163
isAutowireCandidate : Boolean? = null) {
180
-
181
- registrations.add {
182
- val customizer = BeanDefinitionCustomizer { bd ->
183
- scope?.let { bd.scope = scope.name.toLowerCase() }
184
- isLazyInit?.let { bd.isLazyInit = isLazyInit }
185
- isPrimary?.let { bd.isPrimary = isPrimary }
186
- isAutowireCandidate?.let { bd.isAutowireCandidate = isAutowireCandidate }
187
- if (bd is AbstractBeanDefinition ) {
188
- bd.autowireMode = autowireMode.ordinal
189
- }
190
- }
191
-
192
- when (name) {
193
- null -> it.registerBean(T ::class .java, customizer)
194
- else -> it.registerBean(name, T ::class .java, customizer)
164
+
165
+ val customizer = BeanDefinitionCustomizer { bd ->
166
+ scope?.let { bd.scope = scope.name.toLowerCase() }
167
+ isLazyInit?.let { bd.isLazyInit = isLazyInit }
168
+ isPrimary?.let { bd.isPrimary = isPrimary }
169
+ isAutowireCandidate?.let { bd.isAutowireCandidate = isAutowireCandidate }
170
+ if (bd is AbstractBeanDefinition ) {
171
+ bd.autowireMode = autowireMode.ordinal
195
172
}
196
173
}
174
+
175
+ when (name) {
176
+ null -> context.registerBean(T ::class .java, customizer)
177
+ else -> context.registerBean(name, T ::class .java, customizer)
178
+ }
179
+
197
180
}
198
181
199
182
/* *
@@ -216,7 +199,7 @@ open class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) ->
216
199
isPrimary : Boolean? = null,
217
200
autowireMode : Autowire = Autowire .NO ,
218
201
isAutowireCandidate : Boolean? = null,
219
- crossinline function : BeanDefinitionContext . () -> T ) {
202
+ crossinline function : () -> T ) {
220
203
221
204
val customizer = BeanDefinitionCustomizer { bd ->
222
205
scope?.let { bd.scope = scope.name.toLowerCase() }
@@ -228,26 +211,36 @@ open class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) ->
228
211
}
229
212
}
230
213
231
- registrations.add {
232
- val beanContext = BeanDefinitionContext (it)
233
- when (name) {
234
- null -> it.registerBean(T ::class .java,
235
- Supplier { function.invoke(beanContext) }, customizer)
236
- else -> it.registerBean(name, T ::class .java,
237
- Supplier { function.invoke(beanContext) }, customizer)
238
- }
214
+
215
+ when (name) {
216
+ null -> context.registerBean(T ::class .java,
217
+ Supplier { function.invoke() }, customizer)
218
+ else -> context.registerBean(name, T ::class .java,
219
+ Supplier { function.invoke() }, customizer)
239
220
}
221
+
222
+ }
223
+
224
+ /* *
225
+ * Get a reference to the bean by type or type + name with the syntax
226
+ * `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
227
+ * it could be as short as `ref()` or `ref("foo")`.
228
+ * @param name the name of the bean to retrieve
229
+ * @param T type the bean must match, can be an interface or superclass
230
+ */
231
+ inline fun <reified T : Any > ref (name : String? = null) : T = when (name) {
232
+ null -> context.getBean(T ::class .java)
233
+ else -> context.getBean(name, T ::class .java)
240
234
}
241
235
242
236
/* *
243
237
* Take in account bean definitions enclosed in the provided lambda only when the
244
238
* specified profile is active.
245
239
*/
246
- fun profile (profile : String , init : BeanDefinitionDsl .() -> Unit ): BeanDefinitionDsl {
240
+ fun profile (profile : String , init : BeanDefinitionDsl .() -> Unit ) {
247
241
val beans = BeanDefinitionDsl ({ it.activeProfiles.contains(profile) })
248
- beans.init ()
242
+ beans.init = init
249
243
children.add(beans)
250
- return beans
251
244
}
252
245
253
246
/* *
@@ -257,25 +250,21 @@ open class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) ->
257
250
* bean definition block
258
251
*/
259
252
fun environment (condition : ConfigurableEnvironment .() -> Boolean ,
260
- init : BeanDefinitionDsl .() -> Unit ): BeanDefinitionDsl {
253
+ init : BeanDefinitionDsl .() -> Unit ) {
261
254
val beans = BeanDefinitionDsl (condition::invoke)
262
- beans.init ()
255
+ beans.init = init
263
256
children.add(beans)
264
- return beans
265
257
}
266
258
267
259
/* *
268
260
* Register the bean defined via the DSL on the provided application context.
269
261
* @param context The `ApplicationContext` to use for registering the beans
270
262
*/
271
263
override fun initialize (context : GenericApplicationContext ) {
272
- for (registration in registrations) {
273
- if (condition.invoke(context.environment)) {
274
- registration.invoke(context)
275
- }
276
- }
264
+ this .context = context
277
265
for (child in children) {
278
266
child.initialize(context)
279
267
}
268
+ init ()
280
269
}
281
270
}
0 commit comments