Introduce BeanRegistrarDsl
This commit introduces a new BeanRegistrarDsl that supersedes BeanDefinitionDsl which is now deprecated. See BeanRegistrarDslConfigurationTests for a concrete example. See gh-18353
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION")
|
||||
package org.springframework.context.support
|
||||
|
||||
import org.springframework.aot.AotDetector
|
||||
@@ -25,6 +26,8 @@ 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.ParameterizedTypeReference
|
||||
import org.springframework.core.ResolvableType
|
||||
import org.springframework.core.env.ConfigurableEnvironment
|
||||
import org.springframework.core.env.Profiles
|
||||
import java.util.function.Supplier
|
||||
@@ -68,6 +71,7 @@ import java.util.function.Supplier
|
||||
* @see BeanDefinitionDsl
|
||||
* @since 5.0
|
||||
*/
|
||||
@Deprecated(message = "Use BeanRegistrarDsl instead")
|
||||
fun beans(init: BeanDefinitionDsl.() -> Unit) = BeanDefinitionDsl(init)
|
||||
|
||||
/**
|
||||
@@ -79,6 +83,9 @@ fun beans(init: BeanDefinitionDsl.() -> Unit) = BeanDefinitionDsl(init)
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
@Deprecated(
|
||||
replaceWith = ReplaceWith("BeanRegistrarDsl", "org.springframework.beans.factory.BeanRegistrarDsl"),
|
||||
message = "Use BeanRegistrarDsl instead")
|
||||
open class BeanDefinitionDsl internal constructor (private val init: BeanDefinitionDsl.() -> Unit,
|
||||
private val condition: (ConfigurableEnvironment) -> Boolean = { true })
|
||||
: ApplicationContextInitializer<GenericApplicationContext> {
|
||||
@@ -102,6 +109,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
|
||||
/**
|
||||
* Scope enum constants.
|
||||
*/
|
||||
@Deprecated(message = "Use BeanRegistrarDsl instead")
|
||||
enum class Scope {
|
||||
|
||||
/**
|
||||
@@ -120,6 +128,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
|
||||
/**
|
||||
* Role enum constants.
|
||||
*/
|
||||
@Deprecated(message = "Use BeanRegistrarDsl instead")
|
||||
enum class Role {
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.assertj.core.api.ThrowableAssert
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.InitializingBean
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException
|
||||
import org.springframework.beans.factory.config.BeanDefinition
|
||||
import org.springframework.beans.factory.getBean
|
||||
import org.springframework.beans.factory.BeanRegistrarDsl
|
||||
|
||||
/**
|
||||
* Kotlin tests leveraging [BeanRegistrarDsl].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
class BeanRegistrarDslConfigurationTests {
|
||||
|
||||
@Test
|
||||
fun beanRegistrar() {
|
||||
val context = AnnotationConfigApplicationContext(BeanRegistrarKotlinConfiguration::class.java)
|
||||
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
|
||||
assertThatThrownBy(ThrowableAssert.ThrowingCallable { context.getBean<Baz>() }).isInstanceOf(NoSuchBeanDefinitionException::class.java)
|
||||
assertThat(context.getBean<Init>().initialized).isTrue()
|
||||
val beanDefinition = context.getBeanDefinition("bar")
|
||||
assertThat(beanDefinition.scope).isEqualTo(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
assertThat(beanDefinition.isLazyInit).isTrue()
|
||||
assertThat(beanDefinition.description).isEqualTo("Custom description")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun beanRegistrarWithProfile() {
|
||||
val context = AnnotationConfigApplicationContext()
|
||||
context.register(BeanRegistrarKotlinConfiguration::class.java)
|
||||
context.getEnvironment().addActiveProfile("baz")
|
||||
context.refresh()
|
||||
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
|
||||
assertThat(context.getBean<Baz>().message).isEqualTo("Hello World!")
|
||||
assertThat(context.getBean<Init>().initialized).isTrue()
|
||||
}
|
||||
|
||||
class Foo
|
||||
data class Bar(val foo: Foo)
|
||||
data class Baz(val message: String = "")
|
||||
class Init : InitializingBean {
|
||||
var initialized: Boolean = false
|
||||
|
||||
override fun afterPropertiesSet() {
|
||||
initialized = true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(SampleBeanRegistrar::class)
|
||||
internal class BeanRegistrarKotlinConfiguration
|
||||
|
||||
private class SampleBeanRegistrar : BeanRegistrarDsl({
|
||||
registerBean<Foo>()
|
||||
registerBean<Bar>(
|
||||
name = "bar",
|
||||
prototype = true,
|
||||
lazyInit = true,
|
||||
description = "Custom description") {
|
||||
Bar(bean<Foo>())
|
||||
}
|
||||
profile("baz") {
|
||||
registerBean { Baz("Hello World!") }
|
||||
}
|
||||
registerBean<Init>()
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION")
|
||||
package org.springframework.context.support
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
|
||||
Reference in New Issue
Block a user