Let's start with the org.springframework.scala.context.RichApplicationContext. The latter trait provides Scala-friendly versions of the methods from the plain old Spring ApplicationContext. In order to convert ApplicationContext into RichApplicationContext you can use implicit conversion defined in the ApplicationContextConversions.toRichApplicationContext object.
import ApplicationContextConversions._ ... val context : ApplicationContext = ... val nissan = context.bean[Nissan] val xtrail = context.bean[Nissan](name = "xtrail") // Return Map[String,Car]. val allCars = context.beansOfType[Car] // Return Seq[String]. val namesOfCars = context.beanNamesForType[Car]If you like even more syntactic sugar you can use RichListableBeanFactory.appy() notation to retrieve the bean from the context.
import ApplicationContextConversions._ ... val context : ApplicationContext = ... // Syntactic sugar magic! val nissan = context[Nissan] // Same magic by bean name. val xtrail : Nissan = context("xtrail")Keep in mind that org.springframework.scala.context.function.FunctionalConfigApplicationContext extends RichApplicationContext out of the box, so you don't need to use ApplicationContextConversions implicit conversion while working with this type of context.
// We can comment out context's implicit conversions // import ApplicationContextConversions._ ... val context = FunctionalConfigApplicationContext[CarConfig] val nissan = context[Nissan]How do you like new Spring Scala context API? What else would you like Scala-friendly ApplicationContext to have?
Thanks!
ReplyDeleteYou're more than welcome. Stay tuned for more :) .
ReplyDelete