Попробуйте с @Provides
аннотацией
import com.google.inject.{AbstractModule, Guice, Inject, Injector, Provides}
import scala.jdk.CollectionConverters._
trait MyRepository {}
class MyRepositoryImp extends MyRepository {}
trait MyService {}
class MyServiceImp @Inject() (myRepository: MyRepository) extends MyService {}
class Demo @Inject() (myService: MyService) { println("Woohoo, I am wired!") }
class Module extends AbstractModule {
@Provides def repository: MyRepository = new MyRepositoryImp
@Provides def service(repo: MyRepository): MyService = new MyServiceImp(repo)
@Provides def demo(service: MyService): Demo = new Demo(service)
}
object guiceExample extends App {
val injector: Injector = Guice.createInjector(List(new Module).asJava)
val demo = injector.getInstance(classOf[Demo])
}
, которая выводит Woohoo, I am wired!
.