Monday, April 22, 2013

Building functional application context with Spring Scala

The is the second post of mine presenting the latest changes to the bleeding-edge Spring Scala 1.0.0.M2 release of the Spring Scala project from the Spring Source portfolio. As I mentioned in my previous post, my goal is to provide a set of practical examples for Spring Scala users until project reference documentation will be published.

This time I would like to describe the possible ways of creating functional application contexts available in the latest milestone release.

First of all, let me demonstrate the example of simple FunctionalConfigApplicationContext. Quick reminder here - functional application config is a brand new way to define Scala-friendly Spring application context configurations by taking the advantage of Scala functions.
import org.springframework.scala.context.function.FunctionalConfigApplicationContext

class MyConfiguration extends FunctionalConfiguration {

  bean("serviceName") {
    new BusinessService(dao())
  }

  val dao = bean("daoName") {
    new JdbcDao()
  }

}
The simplest way to create application context from the configuration class presented above is to use the companion object of the FunctionalConfigApplicationContext class.
val context = FunctionalConfigApplicationContext[MyConfiguration]
val service = context[BusinessService]
If you want to build application context from multiple configuration classes, the companion FunctionalConfigApplicationContext object might be helpful again.
val context = FunctionalConfigApplicationContext(
  classOf[MyConfiguration],classOf[SomeOtherConfiguration])

val service = context[BusinessService]
At last, but not least, if you need a tighter control over the way the FunctionalConfigApplicationContext is built, you can create it using default constructor and then register the FunctionalConfiguration classes manually.
val context = new FunctionalConfigApplicationContext()
context.registerClass[MyConfiguration]
context.registerClasses(
  classOf[AnotherConfiguration], classOf[YetAnotherConfiguration])
context.refresh()

val service = context[BusinessService]
If you have in mind even better way to create functional configurations in Spring Scala, please just drop me a line. Or even better - issue pull request to the Spring Scala GitHub repository.

Wednesday, April 17, 2013

Rich ApplicationContext in Spring Scala

The latest milestone release (1.0.0.M2) of Spring Scala brings you many of the brand new exciting features. Unfortunately as Spring Scala is still in the early development phase, there is no reference documented dedicated to this library. The documentation will be published before the first stable release of Spring Scala. However as both I and Spring Scala forum have been pinged with the questions about the reference documentation for new Spring Scala features, I decided to write some blog posts describing new goodies delivered to the Spring Scala community. This is the very first of these, demonstrating the awesomeness of the rich BeanFactory and ApplicationContext.

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?

Tuesday, April 2, 2013

Spring Scala 1.0.0.M2 has been released

Arjen Poutsma, open source rock star from SpringSource/VMware, announced today that the second milestone of Spring Scala 1.0.0 has been finally released. I feel emotionally bounded to this release, as I contributed pretty much to it :) .

Spring Scala is the brand new project from the Spring portfolio. Its goal is to provide the first-class bridge between the Spring wiring and the Scala code.

New release includes some nice features making Scala people the happy people including:


  • New JdbcTemplate wrapper. We've dropped the SimpleJdbcTemplate as it has been deprecated in Java.
  • Introduced new "rich" wrappers for the BeanFactory, ListableBeanFactory and ApplicationContext. Also added implicit conversions to these wrappers (following the "Pimp my library" pattern).
  • Introduced new AOP Advice wrappers, with implicit conversions (AdviceConversions)
  • Upgraded to Scala 2.10.2 and Spring 3.2.2. Note that Spring 3.2.2 is required when using the FunctionalConfiguration trait.
  • Added Scala-friendly TransactionSynchronizationManager wrapper supporting pattern-matching style of registering synchronization callbacks. 
  • Changed inner workings of FunctionConfiguration so that it now eagerly initializes beans that require this (BeanFactoryPostProcessors etc).
  • And many more bug fixes and other changes.


Stay tuned for the next Spring Scala milestone!