我尝试使用蛋糕模式进行依赖注入,代码如下:
但编译器抱怨
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
对象,以便依赖注入正常工作?
RequiresFoo
中创建多个AlsoNeedsFoo
的实例,是这样吗? - dratewka