I am trying to use Spring with Scala. I know Autowired works with Scala class, but I am using a web-framework that requires an object and I want to inject a dao into it. I w
None of the previous answears worked for me, but after some struggle I could solve it like this:
@Service
class BeanUtil extends ApplicationContextAware {
def setApplicationContext(applicationContext: ApplicationContext): Unit = BeanUtil.context = applicationContext
}
object BeanUtil {
private var context: ApplicationContext = null
def getBean[T](beanClass: Class[T]): T = context.getBean(beanClass)
}
an then the object:
object MyObject {
val myRepository: MyRepository = BeanUtil.getBean(classOf[MyRepository])
}