Add Kotlin extensions for bean registration and retrieval
Issue: SPR-15048
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package org.springframework.beans.factory
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [BeanFactory] providing [KClass] based API.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object BeanFactoryExtension {
|
||||
|
||||
/**
|
||||
* @see BeanFactory.getBean(Class<T>)
|
||||
*/
|
||||
fun <T : Any> BeanFactory.getBean(requiredType: KClass<T>) = getBean(requiredType.java)
|
||||
|
||||
/**
|
||||
* @see BeanFactory.getBean(String, Class<T>)
|
||||
*/
|
||||
fun <T : Any> BeanFactory.getBean(name: String, requiredType: KClass<T>) =
|
||||
getBean(name, requiredType.java)
|
||||
|
||||
/**
|
||||
* @see BeanFactory.getBean(Class<T>, Object...)
|
||||
*/
|
||||
fun <T : Any> BeanFactory.getBean(requiredType: KClass<T>, vararg args:Any) =
|
||||
getBean(requiredType.java, *args)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package org.springframework.beans.factory
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Extension for [ListableBeanFactory] providing [KClass] based API.
|
||||
*
|
||||
* @since 5.0
|
||||
*/
|
||||
object ListableBeanFactoryExtension {
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory.getBeanNamesForType(Class<?>)
|
||||
*/
|
||||
fun <T : Any> ListableBeanFactory.getBeanNamesForType(type: KClass<T>) =
|
||||
getBeanNamesForType(type.java)
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory.getBeanNamesForType(Class<?>, boolean, boolean)
|
||||
*/
|
||||
fun <T : Any> ListableBeanFactory.getBeanNamesForType(type: KClass<T>,
|
||||
includeNonSingletons: Boolean, allowEagerInit: Boolean) =
|
||||
getBeanNamesForType(type.java, includeNonSingletons, allowEagerInit)
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory.getBeansOfType(Class<T>)
|
||||
*/
|
||||
fun <T : Any> ListableBeanFactory.getBeansOfType(type: KClass<T>) =
|
||||
getBeansOfType(type.java)
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory.getBeansOfType(Class<T>, boolean, boolean)
|
||||
*/
|
||||
fun <T : Any> ListableBeanFactory.getBeansOfType(type: KClass<T>,
|
||||
includeNonSingletons: Boolean, allowEagerInit: Boolean) =
|
||||
getBeansOfType(type.java, includeNonSingletons, allowEagerInit)
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory.getBeanNamesForAnnotation
|
||||
*/
|
||||
fun <T : Annotation> ListableBeanFactory.getBeanNamesForAnnotation(type: KClass<T>) =
|
||||
getBeanNamesForAnnotation(type.java)
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory.getBeansWithAnnotation
|
||||
*/
|
||||
fun <T : Annotation> ListableBeanFactory.getBeansWithAnnotation(type: KClass<T>) =
|
||||
getBeansWithAnnotation(type.java)
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactoryExtension.findAnnotationOnBean
|
||||
*/
|
||||
fun <T : Annotation> ListableBeanFactory.findAnnotationOnBean(beanName:String, type: KClass<T>) =
|
||||
findAnnotationOnBean(beanName, type.java)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user