Add Kotlin extensions for bean registration and retrieval
Issue: SPR-15048
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package org.springframework.context.support
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
||||
import java.util.function.Supplier
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [GenericApplicationContext] providing [KClass] based API and
|
||||
* avoiding specifying a class parameter for the [Supplier] based variant thanks to
|
||||
* Kotlin reified type parameters.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object GenericApplicationContextExtension {
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(Class<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
fun <T : Any> GenericApplicationContext.registerBean(beanClass: KClass<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanClass.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(String, Class<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
fun <T : Any> GenericApplicationContext.registerBean(beanName: String, beanClass: KClass<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(beanName, beanClass.java, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(Class<T>, Supplier<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(supplier: Supplier<T>,
|
||||
vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(T::class.java, supplier, *customizers)
|
||||
}
|
||||
|
||||
/**
|
||||
* @see GenericApplicationContext.registerBean(String, Class<T>, Supplier<T>, BeanDefinitionCustomizer...)
|
||||
*/
|
||||
inline fun <reified T : Any> GenericApplicationContext.registerBean(name: String,
|
||||
supplier: Supplier<T>, vararg customizers: BeanDefinitionCustomizer) {
|
||||
registerBean(name, T::class.java, supplier, *customizers)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.springframework.context.support
|
||||
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Test
|
||||
import org.springframework.context.support.GenericApplicationContextExtension.registerBean
|
||||
import org.springframework.beans.factory.BeanFactoryExtension.getBean
|
||||
import java.util.function.Supplier
|
||||
|
||||
class GenericApplicationContextExtensionTests {
|
||||
|
||||
@Test
|
||||
fun registerBeanWithClass() {
|
||||
val context = GenericApplicationContext()
|
||||
context.registerBean(BeanA::class)
|
||||
context.refresh()
|
||||
assertNotNull(context.getBean(BeanA::class))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun registerBeanWithNameAndClass() {
|
||||
val context = GenericApplicationContext()
|
||||
context.registerBean("a", BeanA::class)
|
||||
context.refresh()
|
||||
assertNotNull(context.getBean("a"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun registerBeanWithSupplier() {
|
||||
val context = GenericApplicationContext()
|
||||
context.registerBean(Supplier { BeanA() })
|
||||
context.refresh()
|
||||
assertNotNull(context.getBean(BeanA::class))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun registerBeanWithNameAndSupplier() {
|
||||
val context = GenericApplicationContext()
|
||||
context.registerBean("a", Supplier { BeanA() })
|
||||
context.refresh()
|
||||
assertNotNull(context.getBean("a"))
|
||||
}
|
||||
|
||||
internal class BeanA
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user