Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala Guice中的依赖项注入-传递参数_Scala_Dependency Injection_Guice - Fatal编程技术网

Scala Guice中的依赖项注入-传递参数

Scala Guice中的依赖项注入-传递参数,scala,dependency-injection,guice,Scala,Dependency Injection,Guice,我有一个使用DI解决的非常简单的场景,但是我找不到合适的示例/文档来帮助我完成。我是Scala/Guice world的新手 当前组件如下所示 trait Foo { } class FooImpl extends A { } trait Bar { val description: String } class BarImpl(val description: String) extends Bar { } class FooImpl extends Foo { Bar

我有一个使用DI解决的非常简单的场景,但是我找不到合适的示例/文档来帮助我完成。我是Scala/Guice world的新手

当前组件如下所示

trait Foo {
}

class FooImpl extends A {

}

trait Bar {
   val description: String
}

class BarImpl(val description: String) extends Bar {

}
class FooImpl extends Foo {
  Bar bar = createBar("Random Bar Value!")
}
现在,我在Foo和Bar之间有一个依赖关系。 所以,通常代码是这样的

trait Foo {
}

class FooImpl extends A {

}

trait Bar {
   val description: String
}

class BarImpl(val description: String) extends Bar {

}
class FooImpl extends Foo {
  Bar bar = createBar("Random Bar Value!")
}
其中
createBar(“Bar!”)
只返回
newbarimpl(“随机条值”)
。当然,为了简洁起见,我删除了工厂/助手

我意识到,当我使用“new”时,这已经脱离了DI范式。我想确保可以基于参数将Bar注入FooImpl。有点像使用工厂。我们如何在Scala/Guice世界中使用DI


我查看了AssistedInjection/命名参数,但我无法理解最终的用法。我认为这是最好的方法,但我不明白应该如何编写/测试它。

好的,这就是我最终的工作方式。为任何想要处理基于Scala的辅助注射的人重新编写这些步骤

Foo可能需要酒吧,但真正需要注射的是酒吧工厂,而不是酒吧

需要创建一个BarFactory,但是实现可以留给Guice。这就是它变得棘手的地方

trait BarFactory {
  def create(msg:String):Bar
}
那么,让我们重温一下Foo和Bar:

@ImplementedBy(classOf[FooImpl])
trait Foo {
  def getBar(msg: String): Bar
}

class FooImpl @Inject() (barFactory: BarFactory) extends Foo {
  override def getBar(msg: String): Bar = {
    barFactory.create(msg)
  }
}

@ImplementedBy(classOf[BarImpl])
trait Bar {
  def echo() : String
}

//Note that we use the @Assisted Annotation Here.
class BarImpl @Inject() (@Assisted msg: String) extends Bar {
  override def echo(): String = msg
}
创建实际工厂是作为模块的一部分完成的

class TempModule extends AbstractModule {
  override def configure(): Unit = {
    install(new FactoryModuleBuilder()
      .implement(classOf[Bar], classOf[BarImpl])
      .build(classOf[BarFactory]))
  }
}

一旦启动,工厂实现将由Guice提供,您应该能够使用工厂创建您的实际实现。

好的,这就是最终对我有效的方法。为任何想要处理基于Scala的辅助注射的人重新编写这些步骤

Foo可能需要酒吧,但真正需要注射的是酒吧工厂,而不是酒吧

需要创建一个BarFactory,但是实现可以留给Guice。这就是它变得棘手的地方

trait BarFactory {
  def create(msg:String):Bar
}
那么,让我们重温一下Foo和Bar:

@ImplementedBy(classOf[FooImpl])
trait Foo {
  def getBar(msg: String): Bar
}

class FooImpl @Inject() (barFactory: BarFactory) extends Foo {
  override def getBar(msg: String): Bar = {
    barFactory.create(msg)
  }
}

@ImplementedBy(classOf[BarImpl])
trait Bar {
  def echo() : String
}

//Note that we use the @Assisted Annotation Here.
class BarImpl @Inject() (@Assisted msg: String) extends Bar {
  override def echo(): String = msg
}
创建实际工厂是作为模块的一部分完成的

class TempModule extends AbstractModule {
  override def configure(): Unit = {
    install(new FactoryModuleBuilder()
      .implement(classOf[Bar], classOf[BarImpl])
      .build(classOf[BarFactory]))
  }
}
一旦启动,工厂实现将由Guice提供,您应该能够使用工厂创建实际的实现