From bf1c179b7fca57af0cf4e51044755176f2cf569d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 30 Nov 2023 16:16:57 +0100 Subject: [PATCH] Allow to set the order from Kotlin bean DSL Closes gh-30849 --- .../context/support/BeanDefinitionDsl.kt | 147 ++++++++++++------ .../context/support/BeanDefinitionDslTests.kt | 23 ++- 2 files changed, 122 insertions(+), 48 deletions(-) diff --git a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt index 26237718128..fb2caa87545 100644 --- a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt +++ b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt @@ -21,6 +21,7 @@ import org.springframework.beans.factory.ObjectProvider import org.springframework.beans.factory.config.BeanDefinition import org.springframework.beans.factory.config.BeanDefinitionCustomizer import org.springframework.beans.factory.getBeanProvider +import org.springframework.beans.factory.support.AbstractBeanDefinition import org.springframework.beans.factory.support.BeanDefinitionReaderUtils import org.springframework.context.ApplicationContextInitializer import org.springframework.core.env.ConfigurableEnvironment @@ -165,6 +166,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition */ @@ -176,7 +178,8 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: Role? = null) { + role: Role? = null, + order: Int? = null) { val customizer = BeanDefinitionCustomizer { bd -> scope?.let { bd.scope = scope.name.lowercase() } @@ -187,6 +190,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit destroyMethodName?.let { bd.destroyMethodName = destroyMethodName } description?.let { bd.description = description } role?. let { bd.role = role.ordinal } + order?.let { bd.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order) } } val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context); @@ -207,6 +211,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition * @param function the bean supplier function + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition */ @@ -219,6 +224,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit destroyMethodName: String? = null, description: String? = null, role: Role? = null, + order: Int? = null, crossinline function: BeanSupplierContext.() -> T) { val customizer = BeanDefinitionCustomizer { bd -> @@ -230,6 +236,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit destroyMethodName?.let { bd.destroyMethodName = destroyMethodName } description?.let { bd.description = description } role?. let { bd.role = role.ordinal } + order?.let { bd.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order) } } @@ -252,6 +259,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2.3 @@ -266,9 +274,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke() } } @@ -288,6 +297,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -302,9 +312,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref()) } } @@ -324,6 +335,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -338,9 +350,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref()) } } @@ -360,6 +373,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -374,9 +388,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref()) } } @@ -396,6 +411,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -410,9 +426,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref()) } } @@ -432,6 +449,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -446,9 +464,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref()) } } @@ -468,6 +487,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -482,9 +502,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -504,6 +525,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -519,9 +541,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -541,6 +564,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -556,9 +580,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -578,6 +603,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -593,9 +619,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -615,6 +642,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -630,9 +658,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -652,6 +681,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -667,9 +697,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -689,6 +720,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -704,9 +736,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -726,6 +759,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -741,9 +775,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -763,6 +798,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -779,9 +815,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -801,6 +838,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -817,9 +855,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -839,6 +878,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -855,9 +895,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -877,6 +918,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -893,9 +935,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -915,6 +958,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -931,9 +975,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -953,6 +998,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -969,9 +1015,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -991,6 +1038,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -1007,9 +1055,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -1029,6 +1078,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -1046,9 +1096,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } @@ -1068,6 +1119,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit * @param destroyMethodName Set the name of the destroy method * @param description Set a human-readable description of this bean definition * @param role Set the role hint for this bean definition + * @param order Set the sort order for the targeted bean * @see GenericApplicationContext.registerBean * @see org.springframework.beans.factory.config.BeanDefinition * @since 5.2 @@ -1085,9 +1137,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit initMethodName: String? = null, destroyMethodName: String? = null, description: String? = null, - role: BeanDefinitionDsl.Role? = null) { + role: BeanDefinitionDsl.Role? = null, + order: Int? = null) { - bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role, order) { f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) } } diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt index e2bb589f80e..50e22b7012e 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt @@ -18,11 +18,13 @@ package org.springframework.context.support import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatExceptionOfType -import org.junit.jupiter.api.fail import org.junit.jupiter.api.Test +import org.junit.jupiter.api.fail import org.springframework.beans.factory.NoSuchBeanDefinitionException import org.springframework.beans.factory.getBean +import org.springframework.beans.factory.getBeanProvider import org.springframework.context.support.BeanDefinitionDsl.* +import org.springframework.core.Ordered import org.springframework.core.env.get import org.springframework.core.testfixture.env.MockPropertySource import java.util.stream.Collectors @@ -196,6 +198,25 @@ class BeanDefinitionDslTests { } catch (ignored: Exception) { } } + + @Test + fun `Declare beans with ordering`() { + val beans = beans { + bean(order = Ordered.LOWEST_PRECEDENCE) { + FooFoo("lowest") + } + bean(order = Ordered.HIGHEST_PRECEDENCE) { + FooFoo("highest") + } + } + + val context = GenericApplicationContext().apply { + beans.initialize(this) + refresh() + } + + assertThat(context.getBeanProvider().orderedStream().map { it.name }).containsExactly("highest", "lowest") + } } class Foo