mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Provide bean conditional registration capabilities in BeanRegistrarDsl
Closes gh-36601
This commit is contained in:
@@ -18,8 +18,8 @@ package org.springframework.beans.factory
|
||||
|
||||
import org.springframework.beans.factory.BeanRegistry.SupplierContext
|
||||
import org.springframework.core.ParameterizedTypeReference
|
||||
import org.springframework.core.ResolvableType
|
||||
import org.springframework.core.env.Environment
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Contract for registering programmatically beans.
|
||||
@@ -364,6 +364,28 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
|
||||
return registry.registerBean(object: ParameterizedTypeReference<T>() {}, customizer)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a bean of the given name is already registered.
|
||||
* @param name the name of the bean
|
||||
* @since 7.1
|
||||
*/
|
||||
fun containsBean(name: String): Boolean = registry.containsBean(name)
|
||||
|
||||
/**
|
||||
* Determine whether a bean of the given type is already registered.
|
||||
* @param beanType the type of the bean
|
||||
* @since 7.1
|
||||
*/
|
||||
fun containsBean(beanType: KClass<*>): Boolean = registry.containsBean(beanType.java)
|
||||
|
||||
/**
|
||||
* Determine whether a bean of the given type is already registered.
|
||||
* @param T the type of the bean
|
||||
* @since 7.1
|
||||
*/
|
||||
inline fun <reified T : Any> containsBean(): Boolean =
|
||||
registry.containsBean(object: ParameterizedTypeReference<T>() {})
|
||||
|
||||
|
||||
/**
|
||||
* Context available from the bean instance supplier designed to give access
|
||||
|
||||
+20
@@ -84,7 +84,13 @@ class BeanRegistrarDslConfigurationTests {
|
||||
assertThat(context.getBeanProvider<Bar>().singleOrNull()).isNotNull
|
||||
}
|
||||
|
||||
@Test
|
||||
fun containsBean() {
|
||||
AnnotationConfigApplicationContext(ContainsBeanRegistrarKotlinConfiguration::class.java)
|
||||
}
|
||||
|
||||
class Foo
|
||||
|
||||
data class Bar(val foo: Foo)
|
||||
data class Baz(val message: String = "")
|
||||
class Init : InitializingBean {
|
||||
@@ -145,4 +151,18 @@ class BeanRegistrarDslConfigurationTests {
|
||||
private class ChainedBeanRegistrar : BeanRegistrarDsl({
|
||||
register(SampleBeanRegistrar())
|
||||
})
|
||||
|
||||
@Configuration
|
||||
@Import(ContainsBeanRegistrar::class)
|
||||
internal class ContainsBeanRegistrarKotlinConfiguration
|
||||
|
||||
private class ContainsBeanRegistrar : BeanRegistrarDsl({
|
||||
assertThat(containsBean("foo")).isFalse()
|
||||
assertThat(containsBean(Foo::class)).isFalse()
|
||||
assertThat(containsBean<Foo>()).isFalse()
|
||||
registerBean<Foo>("foo")
|
||||
assertThat(containsBean("foo")).isTrue()
|
||||
assertThat(containsBean(Foo::class)).isTrue()
|
||||
assertThat(containsBean<Foo>()).isTrue()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user