Allow to set the order from Kotlin bean DSL

Closes gh-30849
This commit is contained in:
Sébastien Deleuze
2023-11-30 16:16:57 +01:00
parent 16ac495084
commit bf1c179b7f
2 changed files with 122 additions and 48 deletions

View File

@@ -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<FooFoo>(order = Ordered.LOWEST_PRECEDENCE) {
FooFoo("lowest")
}
bean<FooFoo>(order = Ordered.HIGHEST_PRECEDENCE) {
FooFoo("highest")
}
}
val context = GenericApplicationContext().apply {
beans.initialize(this)
refresh()
}
assertThat(context.getBeanProvider<FooFoo>().orderedStream().map { it.name }).containsExactly("highest", "lowest")
}
}
class Foo