加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 使用蛋糕模式的Transitive DI

发布时间:2020-12-16 09:57:59 所属栏目:安全 来源:网络整理
导读:我正在尝试使用像这样的蛋糕模式进行依赖注入: trait FooComponent { val foo: Foo trait Foo;}trait AlsoNeedsFoo { this: FooComponent =}trait RequiresFoo { this: FooComponent = val a = new AlsoNeedsFoo with FooComponent{ val foo: this.type#Foo
我正在尝试使用像这样的蛋糕模式进行依赖注入:

trait FooComponent {
  val foo: Foo

  trait Foo;
}

trait AlsoNeedsFoo {
  this: FooComponent =>
}

trait RequiresFoo {
  this: FooComponent =>

  val a = new AlsoNeedsFoo with FooComponent{
    val foo: this.type#Foo = RequiresFoo.this.foo
  }

}

但是编译器抱怨RequiresFoo.this.type #Foo不符合预期的类型this.type #Foo.

所以问题是:是否可以在RequiresFoo中创建一个AlsoNeedsFoo对象,以便依赖注入正常工作?

解决方法

使用蛋糕模式,您不应该实例化其他组件,而是扩展它们.

在你的情况下,如果你需要AlsoNeedsFoo的功能,你应该写这样的东西:

this: FooComponent with AlsoNeedsFoo with ... =>

并将所有内容放在最顶层:

val app = MyImpl extends FooComponent with AlsoNeedsFoo with RequiresFoo with ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读