如何在 scala play 中重写 subling 子模块中的绑定
我有 scala play 多模块应用程序,包含 3 个模块:common、moduleA 和 moduleB,其中 moduleA 和 moduleB 依赖于 common。 ModuleA 和 moduleB 应该为共同定义的特征绑定不同的实现。
如果我尝试以这种方式绑定它:
class ModuleA extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bind(classOf[Foo]).to(classOf[ModuleAFooImpl])
}
}
class ModuleB extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bind(classOf[Foo]).to(classOf[ModuleBFooImpl])
}
}
我收到错误:
1) A binding to Foo was already configured at ModuleB.configure(ModuleB.scala:11) (via modules: com.google.inject.util.Modules$OverrideModule -> ModuleB).
Foo 以这种方式在公共子模块的 ActionBuilder 中使用:
class CustomAction @Inject()(foo: Foo) extends ActionBuilder[Request]{
override def invokeBlock {
...
case Left(_) => Future.successful(foo.onUnauthenticated())
...
}
}
我尝试使用命名绑定,如下所示:
bind(classOf[Foo]).annotatedWith(Names.named("ModuleAFoo")).to(classOf[ModuleBFooImpl])
但它不起作用。
有谁知道是否可以在两个子模块中为相同的特征绑定不同的实现?
I have scala play multimodule application with 3 modules: common, moduleA and moduleB where moduleA and moduleB depend on common.
ModuleA and moduleB should bind different implementation for trait defined in common.
If I try to bind it this way:
class ModuleA extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bind(classOf[Foo]).to(classOf[ModuleAFooImpl])
}
}
class ModuleB extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bind(classOf[Foo]).to(classOf[ModuleBFooImpl])
}
}
I'm getting error:
1) A binding to Foo was already configured at ModuleB.configure(ModuleB.scala:11) (via modules: com.google.inject.util.Modules$OverrideModule -> ModuleB).
Foo is used in ActionBuilder in common submodule in this way:
class CustomAction @Inject()(foo: Foo) extends ActionBuilder[Request]{
override def invokeBlock {
...
case Left(_) => Future.successful(foo.onUnauthenticated())
...
}
}
I've tried to used named binding, like this:
bind(classOf[Foo]).annotatedWith(Names.named("ModuleAFoo")).to(classOf[ModuleBFooImpl])
but it didn't work.
Does anyone know if it's possible to bind different implementations for the same trait in two subling submodules?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论