当前位置: 首页 > 工具软件 > Spring Scala > 使用案例 >

基于Spring Scala的示例bean配置

苏宜人
2023-12-01

最近几天,我一直在将Spring Scala用于玩具项目,我不得不说这是一个很棒的项目,与纯粹基于Spring Java Config的简单配置相比,它可以进一步简化Spring配置。

让我从此处的基于Cake Pattern的示例开始进行演示:

// =======================
// 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

蛋糕模式是指定依赖关系的纯Scala方法。

现在,如果要使用Spring的本机Java配置指定此依赖关系,但以Scala作为语言,那么让我首先从需要连接在一起的组件开始:

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
    }
  }

以及将这些组件与使用该配置的示例连接在一起的配置:

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

进一步使用Spring-Scala项目指定依赖项,配置和示例如下所示:

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

本Wiki所述,Spring Scala项目的实质是从FunctionFunctionConfiguration特性导出的“ bean”方法,可以调用此方法来创建bean,并传入参数以指定(如果需要)bean名称,别名,作用域和一个返回实例化bean的函数。
希望该样本能很好地理解Spring Java Config的简单程度,以及将Spring-Scala项目简化为基于Scala的项目的过程。

翻译自: https://www.javacodegeeks.com/2014/05/spring-scala-based-sample-bean-configuration.html

 类似资料: