Skip to content

Commit c4aea62

Browse files
committed
Update Kotlin bean DSL with new BeanDefinition methods
Issue: SPR-17275
1 parent a680880 commit c4aea62

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.support
1818

19+
import org.springframework.beans.factory.config.BeanDefinition
1920
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
2021
import org.springframework.beans.factory.support.AbstractBeanDefinition
2122
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
@@ -97,11 +98,13 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
9798
* Scope enum constants.
9899
*/
99100
enum class Scope {
101+
100102
/**
101103
* Scope constant for the standard singleton scope
102104
* @see org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON
103105
*/
104106
SINGLETON,
107+
105108
/**
106109
* Scope constant for the standard singleton scope
107110
* @see org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE
@@ -119,11 +122,13 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
119122
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_NO
120123
*/
121124
NO,
125+
122126
/**
123127
* Autowire constant that indicates autowiring bean properties by name
124128
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_BY_NAME
125129
*/
126130
BY_NAME,
131+
127132
/**
128133
* Autowire constant that indicates autowiring bean properties by type
129134
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE
@@ -138,6 +143,41 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
138143

139144
}
140145

146+
147+
/**
148+
* Role enum constants.
149+
*/
150+
enum class Role {
151+
152+
/**
153+
* Role hint indicating that a [BeanDefinition] is a major part
154+
* of the application. Typically corresponds to a user-defined bean.
155+
* @see org.springframework.beans.factory.config.BeanDefinition.ROLE_APPLICATION
156+
*/
157+
APPLICATION,
158+
159+
/**
160+
* Role hint indicating that a [BeanDefinition] is a supporting
161+
* part of some larger configuration, typically an outer
162+
* [org.springframework.beans.factory.parsing.ComponentDefinition].
163+
* [SUPPORT] beans are considered important enough to be aware of
164+
* when looking more closely at a particular
165+
* [org.springframework.beans.factory.parsing.ComponentDefinition],
166+
* but not when looking at the overall configuration of an application.
167+
* @see org.springframework.beans.factory.config.BeanDefinition.ROLE_SUPPORT
168+
*/
169+
SUPPORT,
170+
171+
/**
172+
* Role hint indicating that a [BeanDefinition] is providing an
173+
* entirely background role and has no relevance to the end-user. This hint is
174+
* used when registering beans that are completely part of the internal workings
175+
* of a [org.springframework.beans.factory.parsing.ComponentDefinition].
176+
* @see org.springframework.beans.factory.config.BeanDefinition.ROLE_INFRASTRUCTURE
177+
*/
178+
INFRASTRUCTURE
179+
}
180+
141181
/**
142182
* Declare a bean definition from the given bean class which can be inferred when possible.
143183
*
@@ -148,6 +188,10 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
148188
* @param autowireMode Set the autowire mode, `Autowire.CONSTRUCTOR` by default
149189
* @param isAutowireCandidate Set whether this bean is a candidate for getting
150190
* autowired into some other bean.
191+
* @param initMethodName Set the name of the initializer method
192+
* @param destroyMethodName Set the name of the destroy method
193+
* @param description Set a human-readable description of this bean definition
194+
* @param role Set the role hint for this bean definition
151195
* @see GenericApplicationContext.registerBean
152196
* @see org.springframework.beans.factory.config.BeanDefinition
153197
*/
@@ -156,13 +200,21 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
156200
isLazyInit: Boolean? = null,
157201
isPrimary: Boolean? = null,
158202
autowireMode: Autowire = Autowire.CONSTRUCTOR,
159-
isAutowireCandidate: Boolean? = null) {
203+
isAutowireCandidate: Boolean? = null,
204+
initMethodName: String? = null,
205+
destroyMethodName: String? = null,
206+
description: String? = null,
207+
role: Role = Role.APPLICATION) {
160208

161209
val customizer = BeanDefinitionCustomizer { bd ->
162210
scope?.let { bd.scope = scope.name.toLowerCase() }
163211
isLazyInit?.let { bd.isLazyInit = isLazyInit }
164212
isPrimary?.let { bd.isPrimary = isPrimary }
165213
isAutowireCandidate?.let { bd.isAutowireCandidate = isAutowireCandidate }
214+
initMethodName?.let { bd.initMethodName = initMethodName }
215+
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
216+
description?.let { bd.description = description }
217+
bd.role = role.ordinal
166218
if (bd is AbstractBeanDefinition) {
167219
bd.autowireMode = autowireMode.ordinal
168220
}
@@ -182,6 +234,10 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
182234
* @param autowireMode Set the autowire mode, `Autowire.NO` by default
183235
* @param isAutowireCandidate Set whether this bean is a candidate for getting
184236
* autowired into some other bean.
237+
* @param initMethodName Set the name of the initializer method
238+
* @param destroyMethodName Set the name of the destroy method
239+
* @param description Set a human-readable description of this bean definition
240+
* @param role Set the role hint for this bean definition
185241
* @param function the bean supplier function
186242
* @see GenericApplicationContext.registerBean
187243
* @see org.springframework.beans.factory.config.BeanDefinition
@@ -192,13 +248,21 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
192248
isPrimary: Boolean? = null,
193249
autowireMode: Autowire = Autowire.NO,
194250
isAutowireCandidate: Boolean? = null,
251+
initMethodName: String? = null,
252+
destroyMethodName: String? = null,
253+
description: String? = null,
254+
role: Role = Role.APPLICATION,
195255
crossinline function: () -> T) {
196256

197257
val customizer = BeanDefinitionCustomizer { bd ->
198258
scope?.let { bd.scope = scope.name.toLowerCase() }
199259
isLazyInit?.let { bd.isLazyInit = isLazyInit }
200260
isPrimary?.let { bd.isPrimary = isPrimary }
201261
isAutowireCandidate?.let { bd.isAutowireCandidate = isAutowireCandidate }
262+
initMethodName?.let { bd.initMethodName = initMethodName }
263+
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
264+
description?.let { bd.description = description }
265+
bd.role = role.ordinal
202266
if (bd is AbstractBeanDefinition) {
203267
bd.autowireMode = autowireMode.ordinal
204268
}

0 commit comments

Comments
 (0)