Spring Scala based sample bean configuration
I have been using Spring Scala for a toy project for the last few days and I have to say that it is a fantastic project, it simplifies Spring configuration even further when compared to the already simple configuration purely based on Spring Java Config.
Let me demonstrate this by starting with the Cake Pattern based sample here:
// ======================= // service interfaces trait OnOffDeviceComponent { val onOff: OnOffDevice trait OnOffDevice { def on: Unit def off: Unit } } trait SensorDeviceComponent { val sensor: SensorDevice trait SensorDevice { def isCoffeePresent: Boolean } } // ======================= // service implementations trait OnOffDeviceComponentImpl extends OnOffDeviceComponent { class Heater extends OnOffDevice { def on = println("heater.on") def off = println("heater.off") } } trait SensorDeviceComponentImpl extends SensorDeviceComponent { class PotSensor extends SensorDevice { def isCoffeePresent = true } } // ======================= // service declaring two dependencies that it wants injected trait WarmerComponentImpl { this: SensorDeviceComponent with OnOffDeviceComponent => class Warmer { def trigger = { if (sensor.isCoffeePresent) onOff.on else onOff.off } } } // ======================= // instantiate the services in a module object ComponentRegistry extends OnOffDeviceComponentImpl with SensorDeviceComponentImpl with WarmerComponentImpl { val onOff = new Heater val sensor = new PotSensor val warmer = new Warmer } // ======================= val warmer = ComponentRegistry.warmer warmer.trigger
Cake pattern is a pure Scala way of specifying the dependencies.
Now, if we were to specify this dependency using Spring’s native Java config, but with Scala as the language, let me first start with the components that need to be wired together:
trait SensorDevice { def isCoffeePresent: Boolean } class PotSensor extends SensorDevice { def isCoffeePresent = true } trait OnOffDevice { def on: Unit def off: Unit } class Heater extends OnOffDevice { def on = println("heater.on") def off = println("heater.off") } class Warmer(s: SensorDevice, o: OnOffDevice) { def trigger = { if (s.isCoffeePresent) o.on else o.off } }
and the configuration that wires these components together with a sample that uses this configuration:
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Bean @Configuration class WarmerConfig { @Bean def heater(): OnOffDevice = new Heater @Bean def potSensor(): SensorDevice = new PotSensor @Bean def warmer() = new Warmer(potSensor(), heater()) } import org.springframework.context.annotation.AnnotationConfigApplicationContext val ac = new AnnotationConfigApplicationContext(classOf[WarmerConfig]) val warmer = ac.getBean("warmer", classOf[Warmer]) warmer.trigger
Taking this further to use Spring-Scala project to specify the dependencies, the configuration and a sample look like this:
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Bean import org.springframework.scala.context.function.FunctionalConfiguration class WarmerConfig extends FunctionalConfiguration { val h = bean("heater") { new Heater } val p = bean("potSensor") { new PotSensor } bean("warmer") { new Warmer(p(), h()) } } import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.scala.context.function.FunctionalConfigApplicationContext val ac = FunctionalConfigApplicationContext[WarmerConfig] val warmer = ac.getBean("warmer", classOf[Warmer]) warmer.trigger
The essence of the Spring Scala project as explained in this wiki is the “bean” method derived from the `FunctionalConfiguration` trait, this method can be called to create a bean, passing in parameters to specify, if required, bean name, alias, scope and a function which returns the instantiated bean.
This sample hopefully gives a good appreciation for how simple Spring Java Config is, and how much more simpler Spring-Scala project makes it for Scala based projects.
Reference: | Spring Scala based sample bean configuration from our JCG partner Biju Kunjummen at the all and sundry blog. |