Restituisce il componente creato da Spring per classi non appartenenti a Spring,ad esempio per avere un @Component dentro ad un POJO #spring #kotlin
import org.springframework.context.ApplicationContext
import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContextAware
import org.springframework.stereotype.Service
/**
* Restituisce il componente creato da Spring per classi non appartenenti a Spring,
* ad esempio per avere un @Component dentro ad un POJO
*/
@Service
class BeanUtil: ApplicationContextAware {
@Throws(BeansException::class)
override fun setApplicationContext(applicationContext: ApplicationContext) {
context = applicationContext
}
companion object {
private var context: ApplicationContext? = null
fun <T> getBean(beanClass: Class<T>): T {
return context!!.getBean(beanClass)
}
}
}