简体   繁体   中英

Play 2.5 replacing current.injector in Trait

I am migrating my application to Play 2.5 and I have the following issue:

import play.modules.reactivemongo.ReactiveMongoApi

trait Foo {
  override def reactiveMongoApi: ReactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
  ...
}
object Foo extends Foo

As current is now deprecated, I would like to replace it. However, I can't use @Inject() (val reactiveMongoApi: ReactiveMongoApi) as I am in a Trait. How am I supposed to do it ?

You can do something like this:

import play.modules.reactivemongo.ReactiveMongoApi

trait Foo {
  def reactiveMongoApi: ReactiveMongoApi

  // other methods
}

@Singleton
class FooClass @Inject()(reactiveMongoApi: ReactiveMongoApi) extends Foo {
    // other methods
}

Notice how the property name in FooClass ( reactiveMongoApi ) matches the method defined at the trait Foo . After that, you can declare a module to provide the correct bindings .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM